from attackcti import attack_client

technique_severity = {}
group_techniques = []
group_severity = {}


def get_techniques_id(id):
    global group_techniques
    for technique in group_techniques:
        if id == technique["technique_id"]:
            return technique

def main():
    global group_techniques, group_severity
    client = attack_client()
    group_techniques = client.get_techniques_used_by_all_groups()
    for technique in group_techniques:
        if technique["technique_id"] not in technique_severity.keys():
            technique_severity[technique['technique_id']] = 1
        else:
            technique_severity[technique['technique_id']] += 1
    
    technique_severity_asc = dict(sorted(technique_severity.items(), key=lambda x: x[1]))
    technique_severity_des = dict(reversed(sorted(technique_severity.items(), key=lambda x: x[1])))
    
    i = 0

    for technique in technique_severity_asc:
        technique_info = get_techniques_id(technique)
        print(f"| {technique_info['technique_id']:10} | {technique_severity_des[technique]}")
        i += 1
        if i == 5:
            
            break
    
    i = 0

    for technique in technique_severity_des:
        technique_info = get_techniques_id(technique)
        print(f"| {technique_info['technique_id']:10} | {technique_severity_des[technique]}")
        i += 1
        if i == 5:
            break
    
    for technique in group_techniques:
        if technique["name"] not in group_severity.keys():
            group_severity[technique['name']] = 1
        else:
            group_severity[technique['name']] += 1

    group_severity_des = dict(sorted(group_severity.items(), key=lambda x:(-x[1], x[0])))
    
    needed_group = None
    needed_group_stix = None
    i = 0

    for group in group_severity_des:
        print(f"{group} | {group_severity_des[group]}")
        if i == 22:
            needed_group = group
        i += 1

    print(needed_group)

    for technique in group_techniques:
        if technique['name'] == "APT41" or technique['name'] == "CURIUM" or technique['name'] == "Group5":
            
            for key,item in technique.items():
                with open(f"{technique['name']}.txt", "a") as file:
                    file.write(f"{key} : {item}\n")
            with open(f"{technique['name']}.txt", "a") as file:
                    file.write("-"*100 + "\n")

    for technique in group_techniques:
        if technique['name'] == needed_group:
            needed_group_stix = technique
            for key,item in technique.items():
                with open("need_group.txt", "a") as file:
                    file.write(f"{key} : {item}\n")
            with open("need_group.txt", "a") as file:
                    file.write("-"*100 + "\n")
    
    group_software = client.get_software_used_by_group(needed_group_stix)

    for software in group_software:
        for key,item in software.items():
            with open("need_group_software.txt", "a") as file:
                file.write(f"{key} : {item}\n")
        with open("need_group_software.txt", "a") as file:
                file.write("-"*100 + "\n")
    
    

if __name__ == "__main__":
    main()