import { _decorator, Animation, Color, Component, Node, Sprite, SpriteFrame, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('font_grid_item')
export class font_grid_item extends Component {
    @property(Node) img:Node;
    @property(Node) spr_icon_anim:Node;
    @property(SpriteFrame) green_sf:SpriteFrame;
    private m_call_back = null;
    private m_row:number = 0;
    private m_col:number = 0;
    private _isFinish:boolean = false;
    private _Right:boolean = false;
    private right_font_sf:SpriteFrame;
    @property(Node) img_touch_err:Node;
    public initView(sf:SpriteFrame,sf1:SpriteFrame,row:number,col:number,call_back,isRight:boolean){
        if(isRight){
            this.img.getComponent(Sprite).spriteFrame = sf1;
        }else{
            this.img.getComponent(Sprite).spriteFrame = sf;
        }
        this._Right = isRight;
        this._isFinish = false;
        this.right_font_sf = sf1;
        this.m_row = row;
        this.m_col = col;
        this.m_call_back = call_back;
        this.node.off(Node.EventType.TOUCH_START)
        this.node.on(Node.EventType.TOUCH_START,()=>{
            // console.log("font_grid_item!!",this.m_row,this.m_col)
            if(!this._isFinish){
                this.m_call_back(this);
            }
        },this);
        this.hide();
    }

    public Finish(sf:SpriteFrame){
        this.spr_icon_anim.getComponent(Animation).stop();
        this.spr_icon_anim.active = false;
        this.img.getComponent(Sprite).spriteFrame = sf;
        this.img.getComponent(Sprite).color = Color.WHITE;
        this._isFinish = true;
    }

    public isRight():boolean{
        return this._Right;
    }

    public showErrStatus(){
        this.img_touch_err.active = true;
        tween(this.img_touch_err).delay(0.5).call(()=>{
            this.img_touch_err.active = false;
        }).start();
    }

    public isFinish():boolean{
        return this._isFinish;
    }

    public getRow():number{
        return this.m_row;
    }

    public getCol():number{
        return this.m_col;
    }
    public blinkShow(){
        this.spr_icon_anim.active = true;
        this.spr_icon_anim.getComponent(Animation).play();
    }

    public hide(){
        this.spr_icon_anim.active = false;
        this.spr_icon_anim.getComponent(Animation).stop();
    }

    public rewardShow(){
        this.spr_icon_anim.active = true;
        this.spr_icon_anim.getComponent(Sprite).spriteFrame = this.green_sf;
        this.spr_icon_anim.getComponent(Animation).play();
    }
}


