import { _decorator, assetManager, AudioClip, Button, Color, Component, ImageAsset, Node, Prefab, Sprite, SpriteFrame, Texture2D } from 'cc';
import { config } from '../config';
import { bag_data, scene_item_data } from '../../data/data';
import { res_list } from './res_list';
import { edit_scene } from './edit_scene';
import { main } from '../main';
import { gameManager } from '../run/gameManager';
const { ccclass, property } = _decorator;

@ccclass('control')
export class control extends Component {
    @property(Node) btn_res:Node = null;
    @property(Node) btn_control:Node = null;

    @property(Node) btn_select_scene:Node = null;
    @property(Node) btn_task:Node = null;

    private res_and_control_select_index:number = 0;
    private res_and_control_btn_list:Node[] = null;
    @property(Node) btn_item_list:Node = null;
    @property(Node) btn_bg_list:Node = null;
    @property(Node) btn_component_list:Node = null;
    @property(Node) btn_sound_list:Node = null;
    private res_select_index:number = 0;
    private res_btn_list:Node[] = null;

    @property(Node) item_list:Node = null;
    @property(Node) bg_list:Node = null;
    @property(Node) component_list:Node = null;
    @property(Node) sound_list:Node = null;
    private res_view_list:Node[] = null;
    public static Singleton:control = null;
    private m_bag_data:bag_data = null;
    public static res_map:Map<string,SpriteFrame> = new Map()

    public static mp3_cache:Map<string,AudioClip> = new Map()


    @property(Prefab) prefab_img_item:Prefab = null;

    @property(Prefab) prefab_sound_item:Prefab = null;

    private res_data_list:any[] = []

    @property(Node) res_list:Node = null;

    @property(Node) widget_list:Node = null;

    
    @property(Node) task_list:Node = null;

    @property(Node) scene_list:Node = null;

    private m_main:main = null;

    protected start(): void {
        control.Singleton = this;
    }
    public get_bag_data():bag_data{
        return this.m_bag_data;
    }

    public push_scene_data(data:scene_item_data){
        this.m_bag_data.content.push(data)
    }

    public remove_scene_data(data:scene_item_data){
        let i = this.m_bag_data.content.indexOf(data)
        if(i!=-1){
            let temp = []
            for (let index = 0; index < this.m_bag_data.content.length; index++) {
                const element = this.m_bag_data.content[index];
                if(i===index){

                }else{
                    temp.push(element)
                }
            }
            this.m_bag_data.content = temp;
        }
        
    }

    public init(_main:main){
        this.m_main = _main
        if(this.res_and_control_btn_list==null){
            this.res_and_control_btn_list = []
            this.btn_res.name = `${config.select_res_and_control_type.RES_TYPE}`
            this.res_and_control_btn_list.push(this.btn_res)
            this.btn_control.name = `${config.select_res_and_control_type.CONTROL_TYPE}`
            this.res_and_control_btn_list.push(this.btn_control)

            this.btn_select_scene.name = `${config.select_res_and_control_type.SCENE_SELECT}`
            this.res_and_control_btn_list.push(this.btn_select_scene)
            this.btn_task.name = `${config.select_res_and_control_type.TASK}`
            this.res_and_control_btn_list.push(this.btn_task)
        }
        this.res_and_control_select_index = config.select_res_and_control_type.RES_TYPE;
        if(this.res_btn_list==null){
            this.res_btn_list = [];
            this.btn_item_list.name = `${config.select_res_btn_type.ITEM_LIST}`
            this.res_btn_list.push(this.btn_item_list)

            this.btn_component_list.name = `${config.select_res_btn_type.COMPONENT_LIST}`
            this.res_btn_list.push(this.btn_component_list)

            this.btn_bg_list.name = `${config.select_res_btn_type.BG_LIST}`
            this.res_btn_list.push(this.btn_bg_list)
            
            this.btn_sound_list.name = `${config.select_res_btn_type.SOUND_LIST}`
            this.res_btn_list.push(this.btn_sound_list)

            this.res_view_list = []
            this.res_view_list.push(this.item_list)
            this.res_view_list.push(this.component_list)
            this.res_view_list.push(this.bg_list)
            this.res_view_list.push(this.sound_list)
        }
        this.res_select_index = config.select_res_btn_type.ITEM_LIST;
        this.initBtnEvent()
        this.initViews();
    }

    initViews(){
        for (let index = 0; index < this.res_view_list.length; index++) {
            const element = this.res_view_list[index];
            if(index===config.select_res_btn_type.SOUND_LIST){
                element.getComponent(res_list).initView(this.prefab_sound_item,index,this.res_data_list[index])
            }else{
                element.getComponent(res_list).initView(this.prefab_img_item,index,this.res_data_list[index])
            }
            element.active =  this.res_select_index==index;
        }
    }

    public getResDataByType(type:number){
        for (let index = 0; index < this.res_view_list.length; index++) {
            const element = this.res_view_list[index];
            if(element.getComponent(res_list).getType()==type){
                return element.getComponent(res_list).getData()
            }
        }
        return null;
    }

    update_res_select_views(){
        for (let index = 0; index < this.res_view_list.length; index++) {
            const element = this.res_view_list[index];
            element.active =  this.res_select_index==index;
        }
    }

    public loading_all_resources(data,call_back){
        this.m_bag_data = bag_data.initBagData(data.content);
        if(this.m_bag_data.content==undefined||this.m_bag_data.content==null||this.m_bag_data.content.length<=0){
            this.m_bag_data.content = []
        }
        this.res_data_list = []
        let total = this.m_bag_data.item_list.length+this.m_bag_data.component_list.length+this.m_bag_data.bg_list.length+this.m_bag_data.sound_list.length;
        this.res_data_list.push(this.m_bag_data.item_list)
        this.res_data_list.push(this.m_bag_data.component_list)
        this.res_data_list.push(this.m_bag_data.bg_list)
        this.res_data_list.push(this.m_bag_data.sound_list)
        let count = 0;
        let call = ()=>{
            count++;
            if(count>=total){
                call_back()
            }
        }
        for (let index = 0; index < this.m_bag_data.item_list.length; index++) {
            let url = this.m_bag_data.item_list[index].url;
            let name = this.m_bag_data.item_list[index].name;
            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()
                    control.res_map.set(name,spFrame2)
                    gameManager.res_map.set(url,spFrame2)
                    call()
                }
            });  
        }

        for (let index = 0; index < this.m_bag_data.bg_list.length; index++) {
            let url = this.m_bag_data.bg_list[index].url;
            let name = this.m_bag_data.bg_list[index].name;
            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()
                    control.res_map.set(name,spFrame2)
                    gameManager.res_map.set(url,spFrame2)
                    call()
                }
            });  
        }

        for (let index = 0; index < this.m_bag_data.component_list.length; index++) {
            let url = this.m_bag_data.component_list[index].url;
            let name = this.m_bag_data.component_list[index].name;
            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()
                    control.res_map.set(name,spFrame2)
                    gameManager.res_map.set(url,spFrame2)
                    call()
                }
            });  
        }

        for (let index = 0; index < this.m_bag_data.sound_list.length; index++) {
            let url = this.m_bag_data.sound_list[index].url;
            let name = this.m_bag_data.sound_list[index].name;
            assetManager.loadRemote(url,  (err: any, clip: any)=> {
                clip.addRef()
                if (!err && clip) {  
                    control.mp3_cache.set(name,clip)
                    gameManager.mp3_cache.set(url,clip)
                    call()
                }
            });
        }
    }

    initBtnEvent(){
        for (let index = 0; index < this.res_and_control_btn_list.length; index++) {
            const element = this.res_and_control_btn_list[index];
            this.res_view_list[index].active = false;
            element.off(Node.EventType.TOUCH_END)
            element.on(Node.EventType.TOUCH_END,()=>{
                this.select_res_btn_type_on_click(parseInt(element.name))
            },element)
        }
        this.select_res_btn_type_on_click(this.res_and_control_select_index)

        for (let index = 0; index < this.res_btn_list.length; index++) {
            const element = this.res_btn_list[index];
            element.off(Node.EventType.TOUCH_END)
            element.on(Node.EventType.TOUCH_END,()=>{
                this.select_res_and_control_type_on_click(parseInt(element.name))
            },element)
        }
        this.select_res_and_control_type_on_click(this.res_select_index)

    }


    hide_all(){
        this.task_list.active = false;
        this.widget_list.active = false;
        this.res_list.active = false;
        this.scene_list.active = false;
        this.btn_res.getComponent(Sprite).color = Color.GRAY;
        this.btn_control.getComponent(Sprite).color = Color.GRAY;
        this.btn_select_scene.getComponent(Sprite).color = Color.GRAY;
        this.btn_task.getComponent(Sprite).color = Color.GRAY;
    }
    show_res(){
        this.hide_all();
        this.res_list.active = true;
        this.btn_res.getComponent(Sprite).color = Color.YELLOW;
    }

    show_widget(){
        this.hide_all();
        this.widget_list.active = true;
        this.btn_control.getComponent(Sprite).color = Color.YELLOW;
    }

    show_select_scene(){
        this.hide_all();
        this.scene_list.active = true;
        this.btn_select_scene.getComponent(Sprite).color = Color.YELLOW;
    }

    show_task(){
        this.hide_all();
        this.task_list.active = true;
        this.btn_task.getComponent(Sprite).color = Color.YELLOW;
    }

    select_res_btn_type_on_click(index){
        switch (index) {
            case config.select_res_and_control_type.RES_TYPE:
                this.show_res()
                break;
            case config.select_res_and_control_type.CONTROL_TYPE:
                this.show_widget()
                break;
            case config.select_res_and_control_type.SCENE_SELECT:
                this.show_select_scene()
                break;
            case config.select_res_and_control_type.TASK:
                this.show_task()
                break;
        
        }
        this.res_and_control_select_index = index
    }

    select_res_and_control_type_on_click(index){
        let select_node = this.res_btn_list[index]
        for (let i = 0; i < this.res_btn_list.length; i++) {
            const element = this.res_btn_list[i];
            if(i==index){

            }else{
                element.getComponent(Sprite).color = Color.GRAY;
            }
        }
        select_node.getComponent(Sprite).color = Color.YELLOW;
        this.res_select_index = index
        this.update_res_select_views()
    }
}


