import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { main } from '../main';
import { scene_item_data } from '../../data/data';
import { scene_select_list_item } from './scene_select_list_item';
import { ClientEvent } from '../clientEvent';
import { config } from '../config';
import { tools } from '../tools';
import { Attributes } from './Attributes';
import { show_copy_scene } from './show_copy_scene';
const { ccclass, property } = _decorator;

@ccclass('scene_select_list')
export class scene_select_list extends Component {
    @property(Prefab) scene_select_list_item_prefab:Prefab = null;
    @property(Node) content:Node = null;
    private cur_select_index = 0;
    private m_main:main = null;
    public initView(_main:main){
        this.m_main = _main;
        this.content.removeAllChildren()
        this.onUpdateSceneList()
        ClientEvent.off(config.Event.UpdateSceneList,this.onUpdateSceneList,this)
        ClientEvent.on(config.Event.UpdateSceneList,this.onUpdateSceneList,this)
    }

    onUpdateSceneList(is_move:boolean = false){
        let list = this.m_main.control_view.get_bag_data().content;
        if(is_move==false) {
            this.cur_select_index = list.length>0?list.length-1:0;
        }
        this.content.removeAllChildren()
        for (let index = 0; index < list.length; index++) {
            const element:scene_item_data = list[index];
            let item = instantiate(this.scene_select_list_item_prefab)
            item.parent = this.content;
            let scene_select_list_item_component = item.getComponent(scene_select_list_item)
            scene_select_list_item_component.initView(list.length,element,this.onItemSelect.bind(this),index)
            scene_select_list_item_component.moveCallback(this.onItemMoveClick.bind(this))
            scene_select_list_item_component.copyCallback(this.onItemCopyClick.bind(this))
            
        }
        this.updaetSelectStatus()
    }

    onItemSelect(item:scene_select_list_item){
        if(item.getSelect()){

        }else{
            this.cur_select_index = item.getIndex()
            this.updaetSelectStatus()
        }
    }

    onItemMoveClick(item:scene_select_list_item, is_up:boolean) {
        let list = this.m_main.control_view.get_bag_data().content;
        let c_index = item.getIndex()
        let c_data = list[c_index]
        let move_index=-1;
        if(is_up) {
            move_index = c_index - 1
            if(item.getSelect()) {
                this.cur_select_index -=1
            } else {
                this.cur_select_index +=1
            }
        } else {
            move_index = c_index + 1
            if(item.getSelect()) {
                this.cur_select_index +=1
            } else {
                this.cur_select_index -=1
            }
        }
        if(move_index == -1) {return}
        let move_data = list[move_index]
        list[c_index] = move_data
        list[move_index] = c_data
        this.onUpdateSceneList(true)
    }

    onItemCopyClick(item:scene_select_list_item) {
        let c_index = item.getIndex()
        let list = this.m_main.control_view.get_bag_data().content;
        let c_data = list[c_index]
        // console.log('c_data=',c_data)
        tools.show_copy_scene(c_data.scene_diy_name, (scene_name:string, copy_scene_view:show_copy_scene)=> {  
            let copy_data = JSON.parse(JSON.stringify(c_data))  //深拷贝
            copy_data.scene_diy_name = scene_name
            for(let i=0; i<copy_data.page_list.length; i++) {
                let i_page_item = copy_data.page_list[i]
                i_page_item.att.id = config.getNewId()
                for(let j=0; j<i_page_item.page_list.length; j++) {
                    let j_page_item = i_page_item.page_list[j]
                    j_page_item.att.id = i_page_item.att.id
                }
                for(let j=0; j<i_page_item.page_widget_list.length; j++) {
                    let j_page_widget_item = i_page_item.page_widget_list[j]
                    j_page_widget_item.att.id = config.getNewId()
                }
            }  
            // console.log('copy_data=',copy_data)
            this.m_main.control_view.push_scene_data(copy_data)
            this.onUpdateSceneList()
            copy_scene_view.close()
        })
    }

    updaetSelectStatus(){
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            element.getComponent(scene_select_list_item).setIsSelect(element.getComponent(scene_select_list_item).getIndex()==this.cur_select_index)
            element.getComponent(scene_select_list_item).updatSelectStatus()
        }
        ClientEvent.dispatchEvent(config.Event.UpdateEditScene)
    }

    public getCurSelectScene(){
        if(this.content.children.length<=0){
            return null;
        }
        return this.content.children[this.cur_select_index].getComponent(scene_select_list_item).getData()
    }

    public getCurSelectSceneIndex(){
        return this.cur_select_index; 
    }
}


