import os, sys, argparse, json
# 添加上级目录到 sys.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))

from Initial_Alg_Program.Initial_Alg_Select_Tool import select_crop_size, select_pretrained_model, select_test_cfg_slide

from Initial_Alg_Program.Initial_Alg_Gen_Tool import format_all_data, write_config_to_file, create_dict_by_kwargs, get_var_from_py_file
from Initial_Alg_Program.Initial_Alg_Gen_0_base_ import generate_base_config
from Initial_Alg_Program.Initial_Alg_Gen_1_norm_cfg import generate_norm_cfg
from Initial_Alg_Program.Initial_Alg_Gen_2_data_preprocessor import generate_data_preprocessor
from Initial_Alg_Program.Initial_Alg_Gen_3_model import generate_model_pretrained, generate_model_backbone, generate_model_data_preprocessor, generate_model_decode_head, generate_model_auxiliary_head, generate_model_train_cfg, generate_model_test_cfg
from Initial_Alg_Program.Initial_Alg_Gen_4_optimizer import generate_optim_wrapper, generate_param_scheduler
from Initial_Alg_Program.Initial_Alg_Gen_5_train_dataloader import generate_train_dataloader

if __name__ == '__main__':
    ########### 1.超参数-算法默认参数 ###########
    alg_file_name = 'maskformer_my'

    ########### 2.参数解析 ########### 
    parser = argparse.ArgumentParser(description="Run algorithm with specified parameters.")

    # 添加命令行参数
    parser.add_argument('--alg_name', type=str, required=True, help="Algorithm name (e.g., 'ann')")
    parser.add_argument('--dataset_file_name', type=str, required=True, help="Dataset file name (e.g., 'my_dataset_model')")
    parser.add_argument('--mean', type=str, required=True, help="Mean of dataset (e.g., [1.2, 1.2, 1.2])")
    parser.add_argument('--std', type=str, required=True, help="Mean of dataset (e.g., [1.2, 1.2, 1.2])")
    parser.add_argument('--dataset_num_classes', type=int, required=True, help="Dataset class num (e.g., 36)")
    parser.add_argument('--GPU_num', type=int, required=True, help="Num of GPU to train (e.g., 1)")
    parser.add_argument('--schedule_file_name', type=str, required=True, help="Schedule file name (e.g., 'schedule_4k_check_400')")
    parser.add_argument('--train_type', type=str, required=True, help="Schedule train type (e.g., Epoch or Iteration)")
    parser.add_argument('--train_time_or_epoch_k', type=int, required=True, help="Schedule train time k (e.g., 4)")

    # 解析命令行参数
    args = parser.parse_args()

    # 变量赋值
    alg_name = args.alg_name
    dataset_file_name = args.dataset_file_name
    mean = json.loads(args.mean) # 使用json.loads转换成list
    std = json.loads(args.std) # 使用json.loads转换成list
    num_classes = args.dataset_num_classes
    schedule_file_name = args.schedule_file_name
    train_type = args.train_type
    train_time_or_epoch_k = args.train_time_or_epoch_k
    GPU_num = args.GPU_num
    
    
    ########### 3. 设定参数 ########### 

    _base_ = generate_base_config(alg_file_name=alg_file_name, dataset_file_name=dataset_file_name, schedule_file_name=schedule_file_name) # _base_无算法

    crop_size = select_crop_size(predefined_options=[(512, 512), (640, 640), (512, 1024)]) # 选择切割大小

    data_preprocessor = generate_data_preprocessor(crop_size = crop_size, mean = mean, std = std)
    
    model_data_preprocessor = data_preprocessor # 修改data_preprocessor
    model_data_preprocessor = generate_model_data_preprocessor(model_data_preprocessor=model_data_preprocessor)
    
    norm_cfg = generate_norm_cfg(GPU_num=GPU_num)

    # 3.1. 选择backbone
    model_list = ['torchvision://resnet50', 'torchvision://resnet101', 'pretrain/swin_large-6580f57d.pth', 'pretrain/swin_base-e5c09f74.pth', 'pretrain/swin_small-7ba6d6dd.pth', 'pretrain/swin_tiny-1cdeb081.pth'] 
    selected_model_name, select_pretrained, pretrained_pth, selected_model_info = select_pretrained_model(model_list=model_list)
    
    if selected_model_info['type'] == 'ResNet':
        depth = selected_model_info['depth']
        backbone = create_dict_by_kwargs(depth = depth, type=selected_model_info['type'], norm_cfg=norm_cfg, num_stages=4, frozen_stages=-1, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint=pretrained_pth))
        in_channels = [256, 512, 1024, 2048]
    elif  selected_model_info['type'] == 'swin':
        if selected_model_name == 'pretrain/swin_tiny-1cdeb081.pth':
            in_channels = [96, 192, 384, 768]
            depths=[2, 2, 6, 2]
            backbone = dict(
                _delete_=True,
                type='SwinTransformer',
                pretrain_img_size=224,
                embed_dims=96,
                depths=depths,
                num_heads=[3, 6, 12, 24],
                window_size=7,
                mlp_ratio=4,
                qkv_bias=True,
                qk_scale=None,
                drop_rate=0.,
                attn_drop_rate=0.,
                drop_path_rate=0.3,
                patch_norm=True,
                act_cfg=dict(type='GELU'), # ADD
                use_abs_pos_embed=False, # ADD
                # out_indices=(0, 1, 2, 3),
                with_cp=False,
                init_cfg=dict(type='Pretrained', checkpoint=pretrained_pth))
        if selected_model_name == 'pretrain/swin_small-7ba6d6dd.pth':
            depths=[2, 2, 18, 2]
            in_channels = [96, 192, 384, 768]
            backbone = dict(
                _delete_=True,
                type='SwinTransformer',
                pretrain_img_size=224,
                embed_dims=96,
                depths=depths,
                num_heads=[3, 6, 12, 24],
                window_size=7,
                mlp_ratio=4,
                qkv_bias=True,
                qk_scale=None,
                drop_rate=0.,
                attn_drop_rate=0.,
                drop_path_rate=0.3,
                patch_norm=True,
                act_cfg=dict(type='GELU'), # ADD
                use_abs_pos_embed=False, # ADD
                # out_indices=(0, 1, 2, 3),
                with_cp=False,
                init_cfg=dict(type='Pretrained', checkpoint=pretrained_pth))
        if selected_model_name == 'pretrain/swin_base-e5c09f74.pth':
            depths=[2, 2, 18, 2]
            in_channels = [128, 256, 512, 1024]
            backbone = dict(
                type='SwinTransformer',
                pretrain_img_size=384,
                embed_dims=128,
                depths=depths,
                num_heads=[4, 8, 16, 32],
                window_size=12,
                mlp_ratio=4,
                qkv_bias=True,
                qk_scale=None,
                drop_rate=0.,
                attn_drop_rate=0.,
                drop_path_rate=0.3,
                patch_norm=True,
                act_cfg=dict(type='GELU'), # ADD
                use_abs_pos_embed=False, # ADD
                # out_indices=(0, 1, 2, 3),
                with_cp=False,
                init_cfg=dict(type='Pretrained', checkpoint=pretrained_pth))
        if selected_model_name == 'pretrain/swin_large-6580f57d.pth':
            in_channels = [192, 384, 768, 1536]
            depths=[2, 2, 18, 2]
            backbone = dict(
                type='SwinTransformer',
                pretrain_img_size=384,
                embed_dims=192,
                depths=depths,
                num_heads=[6, 12, 24, 48],
                window_size=12,
                mlp_ratio=4,
                qkv_bias=True,
                qk_scale=None,
                drop_rate=0.,
                attn_drop_rate=0.,
                drop_path_rate=0.3,
                patch_norm=True,
                act_cfg=dict(type='GELU'), # ADD
                use_abs_pos_embed=False, # ADD
                # out_indices=(0, 1, 2, 3),
                with_cp=False,
                init_cfg=dict(type='Pretrained', checkpoint=pretrained_pth))

    # 3.2. decode_head
    decode_head = dict(num_classes=num_classes, in_channels=in_channels,
        loss_cls=dict(class_weight=[1.0] * num_classes + [0.1]))

    # 3.3. optimizer部分
    if selected_model_info['type'] == 'ResNet':
        # optimizer
        embed_multi = dict(lr_mult=1.0, decay_mult=0.0)
        optimizer = dict(
            type='AdamW', lr=0.0001, weight_decay=0.05, eps=1e-8, betas=(0.9, 0.999))
        optim_wrapper = dict(
            _delete_=True,
            type='OptimWrapper',
            optimizer=optimizer,
            clip_grad=dict(max_norm=0.01, norm_type=2),
            paramwise_cfg=dict(
                custom_keys={
                    'backbone': dict(lr_mult=0.1, decay_mult=1.0),
                    'query_embed': embed_multi,
                    'query_feat': embed_multi,
                    'level_embed': embed_multi,
                },
                norm_decay_mult=0.0))
        train_dataloader, batch_size, num_workers = generate_train_dataloader(batch_size_default=4, num_workers_default=4)
    elif selected_model_info['type'] == 'swin':
        # set all layers in backbone to lr_mult=0.1
        # set all norm layers, position_embeding,
        # query_embeding, level_embeding to decay_multi=0.0
        backbone_norm_multi = dict(lr_mult=0.1, decay_mult=0.0)
        backbone_embed_multi = dict(lr_mult=0.1, decay_mult=0.0)
        embed_multi = dict(lr_mult=1.0, decay_mult=0.0)
        custom_keys = {
            'backbone': dict(lr_mult=0.1, decay_mult=1.0),
            'backbone.patch_embed.norm': backbone_norm_multi,
            'backbone.norm': backbone_norm_multi,
            'absolute_pos_embed': backbone_embed_multi,
            'relative_position_bias_table': backbone_embed_multi,
            'query_embed': embed_multi,
            'query_feat': embed_multi,
            'level_embed': embed_multi
        }
        custom_keys.update({
            f'backbone.stages.{stage_id}.blocks.{block_id}.norm': backbone_norm_multi
            for stage_id, num_blocks in enumerate(depths)
            for block_id in range(num_blocks)
        })
        custom_keys.update({
            f'backbone.stages.{stage_id}.downsample.norm': backbone_norm_multi
            for stage_id in range(len(depths) - 1)
        })
        # optimizer
        optimizer = dict(
            type='AdamW', lr=0.0001, weight_decay=0.05, eps=1e-8, betas=(0.9, 0.999))
        optim_wrapper = dict(
            _delete_=True,
            type='OptimWrapper',
            optimizer=optimizer,
            clip_grad=dict(max_norm=0.01, norm_type=2),
            paramwise_cfg={'custom_keys':custom_keys, 'norm_decay_mult':0.0})
        
        # 更新train_dataloader
        train_dataloader, batch_size, num_workers = generate_train_dataloader(batch_size_default=2, num_workers_default=2)

    # 3.5. param_scheduler部分
    param_scheduler = generate_param_scheduler(train_time_or_epoch_k)

    ########### 3.生成各个部分 ########### 
    # 综合model
    model = dict(
        backbone = backbone,
        data_preprocessor = model_data_preprocessor,
        decode_head = decode_head
    )

    ########### 4.程序写入 ###########
    # 算法名称解析：maskformer【Alg】 _ r50【pretrained模型深度】 - d8【_base_/models】 _ 4【GPU数量】 x b2【Batch Size大小】 - 40k【schedule】 _ cityscapes【数据集】 - 512x1024【crop_size】.py
    if selected_model_info['type'] == 'ResNet':
        alg_file_name = f"{alg_name}_r{depth}_{'Pre' if select_pretrained else 'NoPre'}_g{GPU_num}-{schedule_file_name.lstrip('schedule_')}_{dataset_file_name}-{crop_size[0]}x{crop_size[1]}.py"
    elif selected_model_info['type'] == 'swin':
        alg_file_name = f"{alg_name}_swin_{selected_model_info['size']}_{'Pre' if select_pretrained else 'NoPre'}_g{GPU_num}-{schedule_file_name.lstrip('schedule_')}_{dataset_file_name}-{crop_size[0]}x{crop_size[1]}.py"
    output_configs_alg_my_alg = os.path.join(f'./configs/maskformer/', alg_file_name)
    write_config_to_file(output_configs_alg_my_alg, _base_=_base_, norm_cfg=norm_cfg, crop_size=crop_size, data_preprocessor=data_preprocessor, model=model, train_dataloader=train_dataloader, optim_wrapper=optim_wrapper, param_scheduler=param_scheduler)

    # 将信息写入文件
    with open("_temp_.txt", "w") as file:
        json.dump({'alg_infos':{'alg_file_name':alg_file_name,'alg_file_pth':output_configs_alg_my_alg}}, file)
