

import os
import sys
import json





# a=os.path.split("E:\\abc/def.bin")
# a=os.path.splitext("E:\\abc/def/")
# print(a)


def _bytes_to_int(d:bytearray):
    ret=0
    num=len(d)
    for i in d:
        ret>>=8
        ret|=i<<(8*(num-1))
    return ret
def _bytes_to_str(d:bytearray):
    d=d.replace(b'\0xff',b'\0x00')
    text=d.strip(b'\0x00').decode("utf-8")
    return text
def _detail_elf(name:str):
    with open(name,"rb") as f:
        data=f.read()
        off_index=16+4*4
        sh_off=_bytes_to_int(data[off_index:off_index+4])
        off_index+=2*4+3*2
        sh_size=_bytes_to_int(data[off_index:off_index+2])
        off_index+=2
        sh_num=_bytes_to_int(data[off_index:off_index+2])
        addr=sh_off+sh_num*sh_size
        size=len(data)
        ret=[" |文件类型:批检仪/赋码仪主板程序"]
        ret+=[" |打包文件为:"]
        while (addr<size):
            d_size=_bytes_to_int(data[addr:addr+4])
            item_name=data[addr+4:addr+256].decode("utf-8")
            ret.append(" |-"+item_name)
            addr+=d_size
        def get_file(name:str):
            addr=sh_off+sh_num*sh_size
            while (addr<size):
                d_size=_bytes_to_int(data[addr:addr+4])
                item_name=_bytes_to_str(data[addr+4:addr+256])
                if(item_name==name):
                    return data[addr+256:addr+d_size]
                addr+=d_size
            return bytearray()
        def decode_info():
            d=get_file("info.json")
            j=json.loads(d)
            ret.append(" |编译时间:{d}".format(d=j["build_date"]))
            ret.append(" |软件版本:{d}".format(d=j["soft_version"]))
        decode_info()
        return ret

def _detail_axf(name:str):
    ret=[" |文件类型:批检仪/赋码仪主板协处理器程序"]
    return ret
def _detail_bin(name:str):
    ret=[" |文件类型:批检仪/赋码仪小板程序"]
    return ret
def _detail_json(name:str):
    file_name=os.path.split(name)
    def scheme_decode_id(id:int):
        # 日bit0~bit4 月bit5~bit8 年bit9~bit15
        sid=(id>>0)&0x7f
        model=(id>>7)&0x1f
        chip=(id>>12)&0xf
        day=(id>>16)&0x1f
        month=(id>>21)&0xf
        year=((id>>25)&0x7f)+2022
        s="{y}-{m}-{d},chip:{chip},model:{model},id:{id}".format(y=year,m=month,d=day,chip=chip,model=model,id=sid)
        return s
    with open(name,"rb") as f:
        j=json.loads(f.read())
        print("file_name:",file_name)
        if(file_name[-1]!="cfg.json"):
            ret=[" |文件类型:检测方案文件"]
            ret.append(" |方案描述:{d}".format(d=j["PlanBrief"]))
            ret.append(" |方案ID:{id}".format(id=j["PlanID"]))
            ret.append(" |方案ID解析:{s}".format(s=scheme_decode_id(j["PlanID"])))
        else:
            ret=[" |文件类型:批检仪/赋码仪主板配置文件"]
        return ret
def _detail_sh(name:str):
    ret=[" |文件类型:批检仪/赋码仪主板程序启动脚本"]
    return ret
def _detail_lua(name:str):
    ret=[" |文件类型:批检仪/赋码仪检测结果判定脚本"]
    return ret
def _detail_pkt(name:str):
    ret=[" |文件类型:MCU程序在线升级文件"]
    with open(name,"rb") as f:
        data=f.read()
        ret.append(" |打包时间:{d}".format(d=_bytes_to_str(data[4:4+20])))
        ret.append(" |主机接口:{d}".format(d=_bytes_to_str(data[40:40+8])))
        ret.append(" |设备类型:{d}".format(d=_bytes_to_str(data[48:48+12])))
    return ret

def _detail_py(name:str):
    return ["暂未实现"]
def _detail_service(name:str):
    return ["暂未实现"]
def _detail_dtb(name:str):
    return ["暂未实现"]
def _detail_jwt(name:str):
    ret=[" |文件类型:杰华特电子模块芯片程序"]
    with open(name,"rb") as f:
        data=f.read()
        data=data[16*1024:]
        ret.append(" |全固件CRC32:{d}".format(d=hex(_bytes_to_int(data[0:4]))))
        ret.append(" |APP部分CRC32:{d}".format(d=hex(_bytes_to_int(data[4:8]))))
        ret.append(" |文件名称:{d}".format(d=_bytes_to_str(data[8:])))
    return ret

_fun_table={
".elf":_detail_elf,
".axf":_detail_axf,
".bin":_detail_bin,
".json":_detail_json,
".sh":_detail_sh,
".lua":_detail_lua,
".pkt":_detail_pkt,
".py":_detail_py,
".service":_detail_service,
".dtb":_detail_dtb,
".jwt":_detail_jwt
}





def detail(path:str):
    tailfix=os.path.splitext(path)
    file_name=os.path.split(path)
    fun=_fun_table[tailfix[-1]]
    ret=[file_name[-1]]
    if(fun is not None):
        ret+=fun(path)
        return ret
    ret+=["未找到此类文件的解析器"]
    return ret


if __name__ == "__main__":
    print(_bytes_to_int([0x12,0x34,0x56,0x78]))
    print(_bytes_to_int([0x12,0x34]))

