import { _decorator, Component, EditBox, Label, Node, Toggle } from 'cc';
import { event_item_switch_scene_event, scene_item_data } from '../../../data/data';
import { config } from '../../config';
import { main } from '../../main';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('event_switch_scene_event')
export class event_switch_scene_event extends Component {
    @property(Node) lab_name:Node = null;
    @property(Node) btn_select_page:Node = null;
    @property(Node) up_node:Node = null;
    @property(Node) down_node:Node = null;
    @property(Node) left_node:Node = null;
    @property(Node) right_node:Node = null;
    @property(Node) only_once_node:Node = null;
    @property(EditBox) edit_time:EditBox = null;
    private m_data:event_item_switch_scene_event = null;
    private m_scene_data:scene_item_data = null;
    public initView(data:event_item_switch_scene_event){
        this.m_data = data;
        this.m_scene_data = main.Singleton.getEditSceneView().getCurSelectScene()
        this.updateSceneStatus()
        this.updateLabStatus()
        this.updateDirectionNodeStatus()
        this.only_once_node.getComponent(Toggle).isChecked = this.m_data.executeOnlyOnce
        if(data.delay_time == undefined) {
            data.delay_time = 0
        }
        this.edit_time.string = data.delay_time.toString()
    }

    start() {
        this.btn_select_page.on(Node.EventType.TOUCH_END, ()=> {
            tools.show_select_more_scene_page(this.m_scene_data.page_list,this.m_data.binding_page_index,(index:number)=> {
                this.m_data.binding_page_index = index
                this.updateSceneStatus()
                this.updateLabStatus()
            })
        })
        this.up_node.on(Node.EventType.TOUCH_END, ()=> {
            this.change(config.switch_scene_page_direction.up)
        })
        this.down_node.on(Node.EventType.TOUCH_END,()=> {
            this.change(config.switch_scene_page_direction.down)
        })
        this.left_node.on(Node.EventType.TOUCH_END,()=> {
            this.change(config.switch_scene_page_direction.left)
        })
        this.right_node.on(Node.EventType.TOUCH_END,()=> {
            this.change(config.switch_scene_page_direction.right)
        })
        this.only_once_node.on('toggle', ()=>{
            this.m_data.executeOnlyOnce = this.only_once_node.getComponent(Toggle).isChecked
        })
        this.edit_time.node.on('editing-did-ended', ()=> {
            this.m_data.delay_time = parseFloat(this.edit_time.string)
        })
    }

    change(direction = config.switch_scene_page_direction.unknown) {
        if(this.m_data.direction == direction) {
            this.m_data.direction = config.switch_scene_page_direction.unknown
            return
        }
        this.m_data.direction = direction
        this.updateDirectionNodeStatus()
    }

    noCheckedAll() {
        this.up_node.getComponent(Toggle).isChecked = false
        this.down_node.getComponent(Toggle).isChecked = false
        this.left_node.getComponent(Toggle).isChecked = false
        this.right_node.getComponent(Toggle).isChecked = false
    }

    updateSceneStatus() {
        switch (this.m_scene_data.type) {
            case config.Scene_Type_List.many_screen_switch_up_down:
                this.up_node.active = true
                this.down_node.active = true
                this.left_node.active = false
                this.right_node.active = false 
                if(this.m_data.binding_page_index == 0) {
                    this.up_node.active = false
                    if(this.m_data.direction==config.switch_scene_page_direction.up) {
                        this.m_data.direction=config.switch_scene_page_direction.unknown
                        this.up_node.getComponent(Toggle).isChecked = false
                    }
                } else if(this.m_data.binding_page_index >= this.m_scene_data.page_list.length-1) {
                    this.down_node.active = false
                    if(this.m_data.direction==config.switch_scene_page_direction.down) {
                        this.m_data.direction=config.switch_scene_page_direction.unknown
                        this.down_node.getComponent(Toggle).isChecked = false
                    }
                }
                break;
            case config.Scene_Type_List.many_screen_switch_left_right:
                this.up_node.active = false
                this.down_node.active = false
                this.left_node.active = true
                this.right_node.active = true
                if(this.m_data.binding_page_index == 0) {
                    this.left_node.active = false
                    if(this.m_data.direction==config.switch_scene_page_direction.left) {
                        this.m_data.direction=config.switch_scene_page_direction.unknown
                        this.left_node.getComponent(Toggle).isChecked = false
                    }
                } else if(this.m_data.binding_page_index >= this.m_scene_data.page_list.length-1) {
                    this.right_node.active = false
                    if(this.m_data.direction==config.switch_scene_page_direction.right) {
                        this.m_data.direction=config.switch_scene_page_direction.unknown
                        this.right_node.getComponent(Toggle).isChecked = false
                    }
                }
                break;
            default:
                break;
        }
    }

    updateDirectionNodeStatus() {
        this.noCheckedAll()
        this.scheduleOnce(()=> {
            switch (this.m_data.direction) {
                case config.switch_scene_page_direction.up:
                    this.up_node.getComponent(Toggle).isChecked = true
                    break;
                case config.switch_scene_page_direction.down:
                    this.down_node.getComponent(Toggle).isChecked = true
                    break;
                case config.switch_scene_page_direction.left:
                    this.left_node.getComponent(Toggle).isChecked = true
                    break;
                case config.switch_scene_page_direction.right:
                    this.right_node.getComponent(Toggle).isChecked = true
                    break;
                default:
                    break;
            }
        },0.1)
    }

    updateLabStatus() {
        // console.log('this.m_data.binding_page_index=',this.m_data.binding_page_index)
        let string = '请选择切换场景的页数'
        if(this.m_data.binding_page_index != undefined && this.m_data.binding_page_index != -1) {
            string = `选择切换场景的页数是：${this.m_data.binding_page_index + 1}`
        }
        this.lab_name.getComponent(Label).string = string
    }

}

