import { _decorator, Component, Node, Prefab } from 'cc';
import { tools } from '../tools';
import { config } from '../config';
import { control } from './control';
import { main } from '../main';
import { scene_item_data } from '../../data/data';
import { scene_select_list } from './scene_select_list';
import { ClientEvent } from '../clientEvent';
import { cur_edit_scene } from './cur_edit_scene';
import { scene_task } from './task/scene_task';
import { http } from '../http';
import { Attributes } from './Attributes';
import { gameManager } from '../run/gameManager';
const { ccclass, property } = _decorator;

@ccclass('edit_scene')
export class edit_scene extends Component {
    @property(Node) btn_refresh_scene:Node = null;
    @property(Node) btn_add_scene:Node = null;
    @property(Node) btn_run:Node = null;
    @property(Node) btn_run_all:Node = null;
    @property(Node) btn_save:Node = null;
    @property(Node) btn_delete:Node = null;
    @property(Node) scene_select_list:Node = null;
    @property(Node) scene_task:Node = null;
    @property(Prefab) scene_prefab:Prefab = null;
    @property(Node) cur_edit_scene_node:Node = null;
    @property(Node) btn_widget_list:Node = null;
    private m_main:main = null;
    public getMain(){
        return this.m_main;
    }
    public init(_main:main){
        this.m_main = _main
        this.initSceneList()
        this.cur_edit_scene_node.getComponent(cur_edit_scene).initView(this)
        this.scene_select_list.getComponent(scene_select_list).initView(this.m_main)
        this.scene_task.getComponent(scene_task).initView(this)
    }

    public getTaskData(){
        return this.scene_task.getComponent(scene_task).getTaskData()
    }

    public getCurSelectSceneIndex(){
        return  this.scene_select_list.getComponent(scene_select_list).getCurSelectSceneIndex(); 
    }
    public getCurSelectScene(){
        return  this.scene_select_list.getComponent(scene_select_list).getCurSelectScene()
    }
    
    public getCurEditBaseView(){
        return  this.cur_edit_scene_node.getComponent(cur_edit_scene).getCurEditBaseView()
    }

    public getPrefabByType():Prefab{
        return this.scene_prefab;
    }

    initSceneList(){

    }

    onAddScene(data:scene_item_data){
        this.m_main.control_view.push_scene_data(data)
        ClientEvent.dispatchEvent(config.Event.UpdateSceneList)
    }
    protected start(): void {
        this.btn_refresh_scene.on(Node.EventType.TOUCH_END, ()=> {
            ClientEvent.dispatchEvent(config.Event.UpdateEditScene)
        })
        this.btn_add_scene.on(Node.EventType.TOUCH_END,()=>{
            tools.show_add_scene(this.onAddScene.bind(this))
        })
        this.btn_run.on(Node.EventType.TOUCH_END,()=>{
            let curScene = this.getCurSelectScene()
            if(curScene.is_child_scene){
                return tools.showToast("子场景不可以运行")
            }
            // tools.game_run()
            tools.show_loading(()=>{
                tools.game_run(()=>{
                    tools.hide_loading()
                })
            })
        })
        this.btn_run_all.on(Node.EventType.TOUCH_END,()=>{
            tools.game_run_all()
        })
        this.btn_save.on(Node.EventType.TOUCH_END,()=>{
            gameManager.requestSaveEditScene()
        })
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_main.control_view.get_bag_data().content.length<=0){
                tools.showToast("暂无场景可以删除！")
            }else{
                tools.show_dialog("是否删除当前选中的场景？",()=>{
                    this.m_main.control_view.remove_scene_data(this.scene_select_list.getComponent(scene_select_list).getCurSelectScene())
                    ClientEvent.dispatchEvent(config.Event.UpdateSceneList)
                })
            }
        })
        this.btn_widget_list.on(Node.EventType.TOUCH_END,()=>{
            Attributes.Singleton.hideAllAtt()
            ClientEvent.dispatchEvent(config.Event.ShowWidgetList)
        })
    }


}


