import { _decorator, Component, EditBox, instantiate, Node, Prefab, Toggle } from 'cc';
import { attributes_data, scene_item_data, scene_tips_rule_data, ui_att_item } from '../../../data/data';
import { scene_tips_rule_item } from './scene_tips_rule_item';
import { config } from '../../config';
import { Attributes } from '../Attributes';
import { main } from '../../main';
const { ccclass, property } = _decorator;

@ccclass('attributes_scene_setting')
export class attributes_scene_setting extends Component {
    @property(Prefab) itemPb:Prefab = null;

    @property(Node) rule_content:Node = null;
    @property(Node) btn_add_rule:Node = null;

    @property(Node) rule_find_tips:Node = null;
    @property(Node) tips:Node = null;
    @property(Node) answer:Node = null;
    @property(EditBox) edit_des:EditBox = null;
    @property(Node) show_arrow_node:Node = null;
    @property(Toggle) toggle_show_arrow:Toggle = null;
    private call_back = null;
    private m_attributes_call = null;
    private m_attributes_data:attributes_data = null;
    private m_data:scene_tips_rule_data = null;
    public initView(call,attributes_call){

        this.call_back = call;
        this.m_attributes_call = attributes_call;

        this.btn_add_rule.on(Node.EventType.TOUCH_END,()=>{
            this.addRule()
        })
        
        this.edit_des.node.on(EditBox.EventType.EDITING_DID_ENDED,()=>{
            this.m_data.scene_des = this.edit_des.string;
            this.update_att(this.m_data)
        })

        this.toggle_show_arrow.node.on(Toggle.EventType.TOGGLE, ()=>{
            Attributes.is_show_more_scene_arrow = !Attributes.is_show_more_scene_arrow
            if(this.m_attributes_call!=null) {
                this.m_attributes_call(this.m_attributes_data)
            }
        })
    }

    public update_att_info(_main:main, data:attributes_data){
        this.m_attributes_data = data
        let cur_scene_data = _main.edit_scene_view.getCurSelectScene()
        if(this.m_attributes_data.type===config.attributes_type.scene) {
            if(cur_scene_data.type == config.Scene_Type_List.many_screen_switch_up_down||
                cur_scene_data.type == config.Scene_Type_List.many_screen_switch_left_right) {
                    this.show_arrow_node.active = true
                    this.toggle_show_arrow.isChecked = Attributes.is_show_more_scene_arrow
            } else {
                this.show_arrow_node.active = false
            }
        } else {
            this.show_arrow_node.active = false
        }
    }

    public update_att(data:scene_tips_rule_data){
        this.m_data = data;
        this.rule_content.removeAllChildren()
        this.rule_content.destroyAllChildren()
        // if(this.rule_content.children.length===this.m_data.rule_list.length){
        //     for (let index = 0; index < this.rule_content.children.length; index++) {
        //         const element = this.rule_content.children[index];
        //         element.getComponent(scene_tips_rule_item).initView(this.m_data.rule_list[index],this.m_data.rule_list.length,this.onTipsDelete.bind(this),config.attributes_list_type.scene_rule_tips)
        //     }
        // }else{
            for (let index = 0; index < this.m_data.rule_list.length; index++) {
                let item = instantiate(this.itemPb)
                item.parent = this.rule_content
                item.getComponent(scene_tips_rule_item).initView(this.m_data.rule_list[index],index,this.onTipsDelete.bind(this),config.attributes_list_type.scene_rule_tips)
            }
        // }
        this.edit_des.string = this.m_data.scene_des==undefined?"":this.m_data.scene_des;
        this.rule_find_tips.getComponent(scene_tips_rule_item).initView(this.m_data.rule_find_tips,-1,null,config.attributes_list_type.scene_rule_tips)
        this.tips.getComponent(scene_tips_rule_item).initView(this.m_data.tips_data.tips_tips,-1,null,config.attributes_list_type.scene_rule_tips)
        this.answer.getComponent(scene_tips_rule_item).initView(this.m_data.tips_data.tips_answer,-1,null,config.attributes_list_type.scene_rule_tips)
    }


    onTipsDelete(index:number){
        this.m_data.rule_list.splice(index,1)
    }

    addRule(){
        let item = instantiate(this.itemPb)
        item.parent = this.rule_content;
        let data = new ui_att_item
        item.getComponent(scene_tips_rule_item).initView(data,this.m_data.rule_list.length,this.onTipsDelete.bind(this),config.attributes_list_type.scene_rule_tips)
        this.m_data.rule_list.push(data)
    }
}


