#!/usr/bin/env python
# -*- coding: utf-8 -*-
##################################################################
#
# Copyright (c) 2024 CICV, Inc. All Rights Reserved
#
##################################################################
"""
@Authors:           yangzihao(yangzihao@china-icv.cn)
@Data:              2024/04/01
@Last Modified:     2024/04/01
@Summary:           Config json drop duplicate.
"""

import json
import os

def read_json_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return json.load(file)

def compare_leaf_nodes(node1, node2):
    """比较两个叶节点是否相同，不同则返回None"""
    if isinstance(node1, dict) and isinstance(node2, dict):
        # 如果两个节点都是字典，递归比较它们的值
        return {key: compare_leaf_nodes(node1.get(key), node2.get(key)) for key in node1.keys()}
    elif isinstance(node1, list) and isinstance(node2, list):
        # 如果两个节点都是列表，递归比较它们的元素
        return [compare_leaf_nodes(item1, item2) for item1, item2 in zip(node1, node2)]
    elif node1 != node2:
        # 如果值不同，返回None
        return None
    else:
        # 如果值相同，返回原值
        return node1

def compare_json_files(file_paths):
    # 读取第一个JSON文件作为基准
    base_json = read_json_file(file_paths[0])
    # 初始化结果JSON，这里先复制基准JSON
    result_json = base_json.copy()

    # 遍历剩余的文件进行比较
    for file_path in file_paths[1:]:
        # 读取当前文件
        current_json = read_json_file(file_path)
        # 比较基准JSON和当前JSON的叶节点
        temp_result = compare_leaf_nodes(base_json, current_json)
        # 更新result_json以反映不同
        update_dict_with_none(result_json, temp_result)

    return result_json

def update_dict_with_none(base_dict, update_dict):
    """递归地更新base_dict，将update_dict中的None值赋给base_dict对应的位置"""
    for key, value in update_dict.items():
        if isinstance(value, dict):
            # 如果value是字典，递归更新
            update_dict_with_none(base_dict.get(key, {}), value)
        elif value is None:
            # 如果value是None，更新base_dict中对应位置的值为None
            base_dict[key] = None
        elif isinstance(base_dict.get(key), list) and isinstance(value, list):
            # 如果base_dict中的值是列表，且update_dict中的值也是列表，则更新列表元素
            base_dict[key] = [update_value if update_value is None else base_value
                              for base_value, update_value in zip(base_dict[key], value)]

def write_json_file(file_path, data):
    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, indent=4, ensure_ascii=False)

def main():
    # 假设您的JSON文件都在当前目录下的一个名为'json_files'的文件夹中
    json_dir = './jsonFiles'
    file_paths = [os.path.join(json_dir, f) for f in os.listdir(json_dir) if f.endswith('.json')]

    # 比较这些文件
    result_json = compare_json_files(file_paths)

    # 将结果写入新的JSON文件
    output_file_path = './output-2.json'
    write_json_file(output_file_path, result_json)
    print(f'Comparison result saved to {output_file_path}')

if __name__ == '__main__':
    main()