import { _decorator, Component, director, Director, instantiate, Node, Size, UITransform, Vec3 } from 'cc';
import { scene_select_list } from './scene_select_list';
import { edit_scene } from './edit_scene';
import { ClientEvent } from '../clientEvent';
import { config } from '../config';
import { base_view } from './base_view';
import { scroll_scene } from './scroll_scene';
import { tools } from '../tools';
import { widget_item } from './widget_item';
import { scene_item_data, widget_item_data } from '../../data/data';
const { ccclass, property } = _decorator;

@ccclass('cur_edit_scene')
export class cur_edit_scene extends Component {
    private m_edit_scene:edit_scene = null;
    protected start(): void {
        ClientEvent.on(config.Event.UpdateEditScene,this.UpdateEditScene,this)
        ClientEvent.on(config.Event.DragWidgetEnd,this.DragWidgetEnd,this)
        ClientEvent.on(config.Event.RemoveWidget,this.removeWidget,this)
        ClientEvent.on(config.Event.updateWidgetData,this.updateWidgetData,this)
    }
    public initView(_edit_scene:edit_scene){
        this.node.removeAllChildren()
        this.m_edit_scene = _edit_scene;
    }
    DragWidgetEnd(){
        if(this.m_edit_scene.getMain().action_node.children.length>0){
            let base_v = this.getCurEditBaseView()
            if(base_v===null){
                tools.showToast("当前没有创建场景")
            }else{
                let page = base_v.getCurPageView();
                if(page===null){
                    return tools.showToast("当前没有分页")
                }
                let node = instantiate(this.m_edit_scene.getMain().action_node.children[0])
                let bg = page.getComponent(scroll_scene).getBg();
                let pos = bg.getComponent(UITransform).convertToNodeSpaceAR(node.position)
                let data = this.m_edit_scene.getMain().action_node.children[0].getComponent(widget_item).getData()
                node.getComponent(widget_item).initWidgetByScene(data)
                if(data.type===config.Widget_Type_List.UI_TOP||data.type===config.Widget_Type_List.TEXT_SOUND){
                    node.getComponent(UITransform).contentSize = new Size(1080,1920)
                    node.position = new Vec3(1080*0.5,1920*0.5,0);
                }else{
                    node.position = pos;
                }
             
                page.getComponent(scroll_scene).addWidget(node)
                //添加数据
                // let scene_data = this.getCurEditSceneData()
                // let cur_page_num = this.getCurEditBaseView().cur_page();
                // let temp_data =  node.getComponent(widget_item).getData();
                // let cur_page_data =  scene_data.page_list[cur_page_num]
                // let n_data  = new widget_item_data(temp_data.name,temp_data.type)
                // n_data.att = temp_data.att;
                // cur_page_data.page_widget_list.push(n_data)
                director.once(Director.EVENT_AFTER_DRAW,()=>{
                    this.updateWidgetData()
                    // console.log("---",this.getCurEditSceneData())
                })
            }

        }
        
    }
    public updateWidgetData(){
        let base_v = this.getCurEditBaseView()
        let page = base_v.getCurPageView();
        let bg = page.getComponent(scroll_scene).getBg();

        let scene_data = this.getCurEditSceneData()
        let cur_page_num = this.getCurEditBaseView().cur_page();
        let cur_page_data =  scene_data.page_list[cur_page_num]
        let temp = []
        for (let index = 0; index < bg.children.length; index++) {
            let node = bg.children[index]
            let temp_data =  node.getComponent(widget_item).getData(); 
            let data  = new widget_item_data(temp_data.name,temp_data.type)
            data.att = temp_data.att;
            temp.push(data)
        }
        cur_page_data.page_widget_list =temp
    }
    public removeWidget(node:Node){
        // let scene_data = this.getCurEditSceneData()
        // let cur_page_num = this.getCurEditBaseView().cur_page();
        // let cur_page_data =  scene_data.page_list[cur_page_num]
        // let data = node.getComponent(widget_item).getData();
        // let index = cur_page_data.page_widget_list.indexOf(data)
        // console.log(" cur_page_data.page_widget_list", cur_page_data.page_widget_list)
        // if(index!=-1){
        //     cur_page_data.page_widget_list.splice(index,1)
        // }
        this.updateWidgetData()
        node.destroy()
        node.removeFromParent()
        // console.log(" cur_page_data.page_widget_list", cur_page_data.page_widget_list)
    }
    UpdateEditScene(){
        this.node.destroyAllChildren()
        this.node.removeAllChildren()
        let data = this.m_edit_scene.getCurSelectScene()
        if(data!=null){
           let pf = instantiate(this.m_edit_scene.getPrefabByType())
           pf.parent =  this.node
           pf.getComponent(base_view).initView(this.m_edit_scene)
        }
    }

    onClickScene(){
        let base_v = this.getCurEditBaseView()
        if(base_v!=null){
            if(base_v.getCurPageView()!=null){
                ClientEvent.dispatchEvent(config.Event.UpdateAttributes,base_v.getCurPageView().getComponent(scroll_scene).getScenePageAtt())
            }else{
                ClientEvent.dispatchEvent(config.Event.UpdateAttributes,null)
            }
           
        }
    }

    getCurEditSceneData():scene_item_data{
        if(this.node.children.length<=0){
            return null;
        }
        return this.node.children[0].getComponent(base_view).getData()
    }

    getCurEditBaseView():base_view{
        if(this.node.children.length<=0){
            return null;
        }
        return this.node.children[0].getComponent(base_view)
    }

}   


