// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class TxtCell extends cc.Component {

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Color)
    rightColor: cc.Color = null;

    @property(cc.Color)
    wrongColr: cc.Color = null;

    @property(cc.Label)
    lb_info: cc.Label = null;

    @property(cc.Node)
    leftRoot: cc.Node = null;

    @property(cc.Node)
    rightRoot: cc.Node = null;

    @property(cc.SpriteFrame)
    rightSp: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    wrongSp: cc.SpriteFrame = null;
    isRight: any;
    clickCall: Function;
    index: any;


    onLoad() {

        this.node.on("click", this.onClickItem, this)
    }
    onClickItem(handle, side) {

        let parent = side == 2 ? this.rightRoot : this.leftRoot;
        if (this.isRight) {
            this.bg.color = this.rightColor.clone();
            let node = new cc.Node();
            node.addComponent(cc.Sprite);
            node.getComponent(cc.Sprite).spriteFrame = this.rightSp;
            node.parent = parent;

            this.lb_info.node.color = cc.Color.WHITE;
        } else {
            this.bg.color = this.wrongColr.clone();
            let node = new cc.Node();
            node.addComponent(cc.Sprite);
            node.getComponent(cc.Sprite).spriteFrame = this.wrongSp;
            node.parent = parent;
            this.lb_info.node.color = cc.Color.WHITE;
        }

        if (handle) {
            this.clickCall && this.clickCall(this.index);
        }
    }
    init(index, info, isRight, clickCall: Function) {
        this.leftRoot.removeAllChildren();
        this.rightRoot.removeAllChildren();
        this.bg.color = cc.Color.WHITE.clone();
        this.lb_info.string = info;
        this.isRight = isRight;
        this.clickCall = clickCall;
        this.index = index;
        this.lb_info.node.color = cc.Color.WHITE.fromHEX("#35C4E3");
    }


    // update (dt) {}
}
