import { _decorator, Component, Node, resources, Size, Sprite, SpriteFrame, Tween, tween, UITransform, Vec2, Vec3 } from 'cc';
import { ResourceUtil } from '../../framework/resourceUtil';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('ddz_card')
export class ddz_card extends Component {
    @property(Node) spr_card_number:Node;
    @property(Node) spr_card_type:Node;
    @property(Node) spr_card_type_1:Node;
    @property(Node) spr_select_status:Node;
    private m_card:string = null;
    private m_card_key:string = null;
    private m_is_wait_delete_status:boolean = false;
    private m_index:number = 0;
    private m_type:string= "";

    private card_type_dic = {'S':"img_red",'C':"img_black",'H':"img_pian",'D':"img_hua"}
    public initView(card:string,type:string,index:number){
        this.m_index = index;
        this.m_card = config.EnvCard2RealCard[card];
        this.spr_select_status.active = false;
        this.m_card_key = card;
        this.m_type = type;
        ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+this.m_card).then((sf:SpriteFrame)=>{
            this.spr_card_number.getComponent(Sprite).spriteFrame = sf;
        })

        switch (this.m_card) {
            case 'X': //小王
                this.spr_card_type_1.active = false;
                this.spr_card_type.active = true;
                this.spr_card_number.getComponent(UITransform).contentSize = new Size(35,210)
                ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+"img_X").then((sf:SpriteFrame)=>{
                    this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
                })
            break;

            case 'D': //大王
                this.spr_card_type_1.active = false;
                this.spr_card_type.active = true;
                this.spr_card_number.getComponent(UITransform).contentSize = new Size(35,210)
                ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+"img_D").then((sf:SpriteFrame)=>{
                    this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
                })
            break;

            case 'J': //J
            // ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+"img_J").then((sf:SpriteFrame)=>{
            //     this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
            // })
            ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+this.card_type_dic[type]).then((sf:SpriteFrame)=>{
                this.spr_card_type_1.getComponent(Sprite).spriteFrame = sf;
            })
            break;

            case 'Q': //Q
            // ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+"img_Q").then((sf:SpriteFrame)=>{
            //     this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
            // })
            ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+this.card_type_dic[type]).then((sf:SpriteFrame)=>{
                this.spr_card_type_1.getComponent(Sprite).spriteFrame = sf;
            })
            break;
            case 'K': //K
            // ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+"img_K").then((sf:SpriteFrame)=>{
            //     this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
            // })
            ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+this.card_type_dic[type]).then((sf:SpriteFrame)=>{
                this.spr_card_type_1.getComponent(Sprite).spriteFrame = sf;
            })
            break;


            default:
                ResourceUtil.loadSpriteFrameRes(config.ddz_res_path+this.card_type_dic[type]).then((sf:SpriteFrame)=>{
                    // this.spr_card_type.getComponent(Sprite).spriteFrame = sf;
                    this.spr_card_type_1.getComponent(Sprite).spriteFrame = sf;
                })
                break;
        }

        // this.node.on(Node.EventType.MOUSE_LEAVE,()=>{
        //     this.unSelect()
        // },this)
   
    }

    public getCard(){
        return this.m_card_key;
    }

    public isWaitDelete(){
        return this.m_is_wait_delete_status;
    }

    public setDeleteStatus(){
        this.m_is_wait_delete_status = true;
    }

    public getType(){
        return this.m_type;
    }
    public getIndex():number{
        return this.m_index;
    }
    public moveUp(){
        if(this.node.position.x>0){
            this.unSelect()
            this.node.position = new Vec3(0,this.node.position.y,this.node.position.z)
        }else{
            this.spr_select_status.active = false;
            tween(this.node).to(0.2,{position:new Vec3(10,this.node.position.y,this.node.position.z)}).start()
        }
    }

    public onSelect(){
        Tween.stopAllByTarget(this.node)
        // this.node.position = new Vec3(0,this.node.position.y,this.node.position.z)
        this.spr_select_status.active = true;
    }

    public unSelect(){
        Tween.stopAllByTarget(this.node)
        // this.node.position = new Vec3(0,this.node.position.y,this.node.position.z)
        this.spr_select_status.active = false;
    }
    public IsSelectStatus():boolean{
        return this.spr_select_status.active;
    }

    public IsOutCard():boolean{
        return this.node.position.x>0;
    }
}


