import re
import os

from collections import Counter

import load_cfg


replace_dict = {'KCLV_YF115': 800, 'launchClamp1': 200}


def get_craft_price(craft_name):
    global part_price_dict
    file_path = os.path.join('data', f'{craft_name}.craft')
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    result_list = [item.replace('.', '_') for item in re.findall(r'part = (.*?)_.*', content)]
    price = 0
    for result in result_list:
        result = result.replace('2_25m', '2.25m').replace('2_6m', '2.6m')
        price += part_price_dict[result]
        # print(result, part_price_dict[result])
    print(craft_name, price)


def diff_part(craft1, craft2):
    file_path = os.path.join('data', f'{craft1}.craft')
    with open(file_path, 'r', encoding='utf-8') as f:
        content1 = f.read()
    file_path = os.path.join('data', f'{craft2}.craft')
    with open(file_path, 'r', encoding='utf-8') as f:
        content2 = f.read()
    part_list1 = [item.replace('.', '_') for item in re.findall(r'part = (.*?)_.*', content1)]
    part_list2 = [item.replace('.', '_') for item in re.findall(r'part = (.*?)_.*', content2)]

    x1 = dict(Counter(part_list1))
    x2 = dict(Counter(part_list2))

    for key, item in x1.items():
        if key in x2:
            num = x1[key]
            x1[key] -= num
            x2[key] -= num

    for key, item in x2.items():
        if key in x1:
            num = x2[key]
            x1[key] -= num
            x2[key] -= num
    in_1_list = []
    in_2_list = []



if __name__ == "__main__":
    KIU_FILE_PATH = r'D:\\My Coding Project\\KIU-Pack\\KIU\KIU_Chinese_Launch_Vehicle_pack\\Parts'
    part_cfg_database = load_cfg.load_part_cfg(KIU_FILE_PATH)
    part_price_dict = {key: int(item['cost']) for key, item in part_cfg_database.items()}
    part_price_dict.update(replace_dict)

    get_craft_price('KIU-CZ-7A（双星构型）')
    get_craft_price('KIU-CZ-7')
    get_craft_price('KIU-CZ-8')
    get_craft_price('KIU-CZ-8Y2')
    get_craft_price('KIU-CZ-3B')
    get_craft_price('KIU-CZ-6')
    get_craft_price('KIU-CZ-6A')
    get_craft_price('KIU-YL-1')
    get_craft_price('Zhuque-2')
    get_craft_price('KIU-CZ-10')

    # get_craft_price('CZ-3B')
    # get_craft_price(('CZ-8'))
    diff_part('KIU-CZ-7A（双星构型）', 'CZ-7A')

    print('done')
