import { _decorator, assetManager, Color, Component, ImageAsset, instantiate, Node, Prefab, resources, Size, Sprite, SpriteFrame, Texture2D, Vec2 } from 'cc';
import { main } from './main';
import { toast } from './toast';
import { add_scene } from './edit/add_scene';
import { dialog_view } from './dialog_view';
import { scene_select_list } from './edit/scene_select_list';
import { sceme_type_select_item } from './sceme_type_select_item';
import { slide_dir_select } from './edit/slide_dir_select';
import { click_type_select } from './click_type_select';
import { add_event } from './edit/event/add_event';
import { add_task } from './edit/task/add_task';
import { add_animation } from './edit/animation/add_animation';
import { att_ani_data, event_item, widget_item_data } from '../data/data';
import { select_widget_list } from './edit/event/select_widget_list';
import { select_animation } from './edit/animation/select_animation';
import { show_widget_list } from './edit/show_widget_list';
import { select_ui_top_type } from './edit/uiWidget/select_ui_top_type';
import { show_select_child_scene } from './edit/uiWidget/show_select_child_scene';
import { show_select_evele_list } from './edit/show_select_evele_list';
import { select_collect_event } from './edit/event/select_collect_event';
import { select_res_list } from './select_res_list';
import { select_task_zhao_xi_jie } from './edit/task/select_task_zhao_xi_jie';
import { show_select_z_index } from './edit/attributes/show_select_z_index';
import { game_run } from './run/game_run';
import { config } from './config';
import { edit_event } from './edit/event/edit_event';
import { select_sound_list } from './edit/select_sound_list';
import { select_task_da_guai } from './edit/task/select_task_da_guai';
import { edit_event_view } from './edit/event/edit_event_view';
import { tip_type_select } from './edit/tip_type_select';
import { show_copy_scene } from './edit/show_copy_scene';
import { show_select_more_scene_page } from './edit/show_select_more_scene_page';
import { select_task_dai_dao_ju } from './edit/task/select_task_dai_dao_ju';
import { set_color } from './edit/set_color';
import { show_setup_res_anchor } from './edit/show_setup_res_anchor';
import { dialog_input } from './dialog_input';
export class tools  {

    public static show_dialog(title="是否确定？",sure_callback,cancel_callback=null){
        resources.load("prefab/dialog_view",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(dialog_view).show(title,sure_callback,cancel_callback)
        })
    }

    public static show_dialog_input(content:string,sure_callback,cancel_callback=null){
        resources.load("prefab/dialog_input",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(dialog_input).show(content,sure_callback,cancel_callback)
        })
    }

    public static show_loading(call=null){
        resources.load("prefab/loading",Prefab,(err, prefab)=>{
            if(!main.loading_view){
                let node = instantiate(prefab);
                node.name = "loading"
                node.parent = main.g_canvas.node;
                main.loading_view = node;
                if(call!=null){
                    call()
                }
            }

        })
    }
    
    public static hide_loading(){
        if(main.loading_view!=null){
            main.loading_view.removeFromParent()
            main.loading_view =null;
        }
    }

    public static showToast(text:string){
        resources.load("prefab/toast",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.name = "toast"
            node.parent = main.g_canvas.node;
            node.getComponent(toast).show(text)
        })
    }

    public static show_add_scene(call){
        resources.load("prefab/add_scene",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(add_scene).initView(call)
        })
    }

    public static add_scene_page(call){
        resources.load("prefab/sceme_type_select_item",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(sceme_type_select_item).initView(call)
        }) 
    }

    public static show_copy_scene(sceneName:string,call) {
        resources.load("prefab/show_copy_scene", Prefab, (err, prefab)=> {
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_copy_scene).initView(sceneName,call)
        })
    }

    public static show_slide_dir_select(call){
        resources.load("prefab/slide_dir_select",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(slide_dir_select).show(call)
        }) 
    }

    public static show_tip_type_select(call){
        resources.load("prefab/tip_type_select",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(tip_type_select).show(call)
        }) 
    }

    public static show_click_type_select(call){
        resources.load("prefab/click_type_select",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(click_type_select).show(call)
        }) 
    }

    public static show_add_event(call){
        resources.load("prefab/add_event",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(add_event).show(call)
        }) 
    }

    public static show_add_task(call){
        resources.load("prefab/add_task",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(add_task).show(call)
        }) 
    }

    public static show_add_animation(list,call){
        resources.load("prefab/add_animation",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(add_animation).show(list,call)
        }) 
    }

    public static show_select_widget_list(list:widget_item_data[],call,id:number=-1){
        resources.load("prefab/select_widget_list",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_widget_list).show(list,call,id)
        }) 
    }

    public static show_widget_list(list){
        resources.load("prefab/show_widget_list",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_widget_list).show(list)
        }) 
    }

    public static show_select_animation_list(list:att_ani_data[],call,id:number=-1){
        resources.load("prefab/select_animation",Prefab,(err, prefab)=>{
            let node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_animation).show(list,call,id)
        }) 
    }


    public static show_select_ui_top_type(call){
        resources.load("prefab/ui/select_ui_top_type",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_ui_top_type).show(call)
        }) 
    }

    public static show_select_child_scene(list,call){
        resources.load("prefab/ui/show_select_child_scene",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_select_child_scene).show(list,call)
        }) 
    }

    public static show_select_evele_list(list,call){
        if(list.length == 0) {
            return tools.showToast('任务事件列表是空的')
        }
        resources.load("prefab/show_select_evele_list",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_select_evele_list).show(list,call)
        }) 
    }

    public static show_setup_res_anchor(img_sf:SpriteFrame,img_size:Size,img_anchor:Vec2,callback:Function) {
        resources.load("prefab/show_setup_res_anchor",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_setup_res_anchor).show(img_sf,img_size,img_anchor,callback)
        })
    }

    public static select_collect_event(list,call,event_id,event_list_id){
        resources.load("prefab/ui/select_collect_event",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_collect_event).show(list,call,event_id,event_list_id)
        }) 
    }

    public static select_res_list(call){
        resources.load("prefab/ui/select_res_list",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_res_list).initView(call)
        }) 
    }

    public static select_sound_list(call){
        resources.load("prefab/ui/select_sound_list",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_sound_list).initView(call)
        }) 
    }

    public static select_task_zhao_xi_jie(data){
        resources.load("prefab/task/select_task_zhao_xi_jie",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_task_zhao_xi_jie).show(data)
        }) 
    }
    public static select_task_da_guai(data){
        resources.load("prefab/task/select_task_da_guai",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_task_da_guai).show(data)
        }) 
    }

    public static select_task_dai_dao_ju(data){
        resources.load("prefab/task/select_task_dai_dao_ju",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(select_task_dai_dao_ju).show(data)
        }) 
    }

    public static show_select_z_index(call){
        resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_select_z_index).show(call)
        }) 
    }

    public static show_edit_event_view(){
        resources.load("prefab/ui/edit_event_view",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(edit_event_view).show()
        }) 
    }

    public static show_select_dir(call){
        resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_select_z_index).showDir(call)
        }) 
    }

    public static show_select_more_scene_page(data,cur_index,call) {
        resources.load("prefab/show_select_more_scene_page",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(show_select_more_scene_page).show(data,cur_index,call)
        }) 
    }

    public static loadUrl(url:string,spr:Sprite,call=null){
        if(url.length<=0){
            return null;
        }
        assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
            if (!err && imageAsset2) {        
                const texture = new Texture2D();
                texture.image = imageAsset2;
                let spFrame2 = new SpriteFrame();     
                spFrame2.texture = texture;
                if(spr!=null) {
                    spr.spriteFrame = spFrame2;
                }
                if(call!=null) {
                    call(spFrame2)
                }
            } else {
                if(call!=null) {
                    call(null)
                }
            }
        }); 
    }

    public static loadSceneImg(url:string,call){
        if(url.length<=0){
            return null;
        }
        assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
            if (!err && imageAsset2) {        
                const texture = new Texture2D();
                texture.image = imageAsset2;
                let spFrame2 = new SpriteFrame();     
                spFrame2.texture = texture;
                spFrame2.addRef()
                call({"url":url,"sf":spFrame2})
            }
        }); 
    }

    public static loadSceneMp3(url:string,call){
        if(url.length<=0){
            return null;
        }
        assetManager.loadRemote(url,  (err: any, clip: any)=> {
            clip.addRef()
            if (!err && clip) {  
                call({"url":url,"clip":clip})
            }
        });
    }


    public static game_run(callback=null){
        resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(game_run).run(callback)
        }) 
    }

    public static game_run_all(){
        resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(game_run).runAll()
        }) 
    }

    public static isWidgetType(type:number){
        return type!=config.Widget_Type_List.TEXT_SOUND
    }

    public static show_edit_event(type:number,data:event_item,delete_call){
        resources.load("prefab/edit_event",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(edit_event).show(type,data,delete_call)
        }) 
    }

    // 去除左边和右边空格
    public static trimLeftAndRightblank(str: string):string {
        const reg = /^\s+|\s+$/g;
        return str.replace(reg, '')
    }

    public static show_set_color(color:Color,call){
        resources.load("prefab/set_color",Prefab,(err, prefab)=>{
            let node:Node = instantiate(prefab);
            node.parent = main.g_canvas.node;
            node.getComponent(set_color).show(color,call)
        }) 
    }
}


