import { _decorator, Component, instantiate, Node, Prefab, Size, Sprite, UITransform, Vec3 } from 'cc';
import { ui_base } from './ui_base';
import { interact_input_data, ui_att_item } from '../../../data/data';
import { tools } from '../../tools';
import { gameManager } from '../gameManager';
import { ui_input_box_number_item } from './ui_input_box_number_item';
const { ccclass, property } = _decorator;

@ccclass('ui_interact_input')
export class ui_interact_input extends ui_base {
    @property(Node) input_default:Node = null;
    @property(Node) input_fail:Node = null;
    @property(Node) input_right:Node = null;
    @property(Node) input_box:Node = null;
    @property(Node) input_box_node:Node = null;
    @property(Prefab) ui_input_box_number_item_prefab:Prefab = null;
    @property(Node) input_btn_0:Node = null;
    @property(Node) input_btn_1:Node = null;
    @property(Node) input_btn_2:Node = null;
    @property(Node) input_btn_3:Node = null;
    @property(Node) input_btn_4:Node = null;
    @property(Node) input_btn_5:Node = null;
    @property(Node) input_btn_6:Node = null;
    @property(Node) input_btn_7:Node = null;
    @property(Node) input_btn_8:Node = null;
    @property(Node) input_btn_9:Node = null;
    @property(Node) input_btn_x:Node = null;
    @property(Node) input_btn_j:Node = null;
    private mInputList:Node[] = []
    private mInputData:interact_input_data = null;
    private mInputAttList:Map<string,ui_att_item> = new Map;
    private btnArrList:ui_att_item[] = []
    private answer_list:string[] = []
    private input_answer_list:string[] = []
    protected init(): void {
        this.mInputData = this.mTopData._interact_input_data;
        if(this.mInputData===null){
            return tools.showToast("配置ui 输入类型错误！")
        }
        this.loadBg(this.mInputData.bg)
        this.answer_list= this.mInputData.answer.split(",")
        this.initInputBtns()
        this.initInputStatusView()
    }

    protected onShow(): void {
        this.input_default.active = true;
        this.input_fail.active =false;
        this.input_right.active = false;
        this.input_box_node.removeAllChildren()
        this.input_answer_list = []
    }

    onInputBtnClick(any_data){
        let name:string = any_data.target.name.toString()
        let input_number = name.substring(4,name.length)
        this.input_answer_list.push(input_number)
        this.InputNumber(input_number)
        if(this.input_answer_list.length>=this.answer_list.length){
            console.log("input--",this.input_answer_list,this.answer_list)
            if(this.input_answer_list.toString()==this.answer_list.toString()){
                this.input_right.active = true;
                this.scheduleOnce(()=>{
                    this.onFinishEvent()
                },1)
            }else{
                this.input_fail.active = true;
                this.scheduleOnce(()=>{
                    this.onFialEvent()
                },1)
            }
        }
    }

  

    InputNumber(name:string){
        let input_number = instantiate(this.ui_input_box_number_item_prefab)
        input_number.parent = this.input_box_node;
        let sf = gameManager.getCacheSpriteFrameByName(this.mInputAttList.get(name).res)
        input_number.getComponent(ui_input_box_number_item).initView(sf,this.mInputData.font,name);
    }

    initInputStatusView(){
        gameManager.initUiBaseAtt(this.input_default,this.mInputData.input_default)
        this.input_fail.getComponent(Sprite).spriteFrame =  gameManager.getCacheSpriteFrameByName(this.mInputData.input_fail.res)
        this.input_fail.getComponent(UITransform).setContentSize(this.input_default.getComponent(UITransform).contentSize)
        this.input_fail.position = this.input_default.position

        this.input_right.getComponent(Sprite).spriteFrame =  gameManager.getCacheSpriteFrameByName(this.mInputData.input_right.res)
        this.input_right.getComponent(UITransform).setContentSize(this.input_default.getComponent(UITransform).contentSize)
        this.input_right.position = this.input_default.position
        this.input_right.active =false;
        this.input_fail.active = false;
    }

    initInputBtns(){
        this.mInputList.push(this.input_btn_0)
        this.mInputList.push(this.input_btn_1)
        this.mInputList.push(this.input_btn_2)
        this.mInputList.push(this.input_btn_3)
        this.mInputList.push(this.input_btn_4)
        this.mInputList.push(this.input_btn_5)
        this.mInputList.push(this.input_btn_6)
        this.mInputList.push(this.input_btn_7)
        this.mInputList.push(this.input_btn_8)
        this.mInputList.push(this.input_btn_9)
        this.mInputList.push(this.input_btn_x)
        this.mInputList.push(this.input_btn_j)


        this.btnArrList.push(this.mInputData.att_0)
        this.btnArrList.push(this.mInputData.att_1)
        this.btnArrList.push(this.mInputData.att_2)
        this.btnArrList.push(this.mInputData.att_3)
        this.btnArrList.push(this.mInputData.att_4)
        this.btnArrList.push(this.mInputData.att_5)
        this.btnArrList.push(this.mInputData.att_6)
        this.btnArrList.push(this.mInputData.att_7)
        this.btnArrList.push(this.mInputData.att_8)
        this.btnArrList.push(this.mInputData.att_9)
        this.btnArrList.push(this.mInputData.att_xing)
        this.btnArrList.push(this.mInputData.att_jing)
        //
        this.mInputAttList.set("0",this.mInputData.image_0)
        this.mInputAttList.set("1",this.mInputData.image_1)
        this.mInputAttList.set("2",this.mInputData.image_2)
        this.mInputAttList.set("3",this.mInputData.image_3)
        this.mInputAttList.set("4",this.mInputData.image_4)
        this.mInputAttList.set("5",this.mInputData.image_5)
        this.mInputAttList.set("6",this.mInputData.image_6)
        this.mInputAttList.set("7",this.mInputData.image_7)
        this.mInputAttList.set("8",this.mInputData.image_8)
        this.mInputAttList.set("9",this.mInputData.image_9)
        this.mInputAttList.set("xing",this.mInputData.image_xing)
        this.mInputAttList.set("jing",this.mInputData.image_jing)
        for (let index = 0; index < this.mInputList.length; index++) {
            const btn = this.mInputList[index];
            let btn_data = this.btnArrList[index];
            btn.getComponent(UITransform).setContentSize(new Size(btn_data.width,btn_data.height))
            btn.position = new Vec3(btn_data.x,btn_data.y)
            btn.on(Node.EventType.TOUCH_START,this.onInputBtnClick.bind(this))
        }
    }
}


