import Main from "../../../main/Main";
import { __RoleIcon } from "../../data/sdata/SManage";

/**
 * 头像/框
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class RoleIconItem extends cc.Component {

    @property(cc.Sprite)
    mIcon: cc.Sprite = null;//图标

    @property(cc.Sprite)
    mFrame: cc.Sprite = null;//底框

    @property(cc.Node)
    mCheck: cc.Node = null;//选中

    @property(cc.Node)
    mLock: cc.Node = null;//锁

    public roleIcon:__RoleIcon

    private callback:(frameIcon:RoleIconItem)=>void

    public setCallback(callback:(frameIcon:RoleIconItem)=>void){
        this.callback = callback
    }

    public onclick(){
        if(this.callback){
            this.callback(this)
        }
    }
    public loadIcon(main:Main){
        this.mCheck.active = false
        this.mLock.active = false
        cc.resources.load('icon/role_head/'+this.roleIcon.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );

        // //载入角色底框
        if(main){
            let role = main.player.role
            let roleFrame = main.sManage.getRoleIconById(role.frame)
            if(roleFrame){
                cc.resources.load('icon/role_head/txk/'+roleFrame.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
                    if(err){
                        cc.error(err);
                    }else{
                       this.mFrame.spriteFrame = spriteFrame;
                    }
                } );
            }
        }
        
    }
    public loadFrame(main:Main){
        this.mCheck.active = false
        this.mLock.active = false
        cc.resources.load('icon/role_head/txk/'+this.roleIcon.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mFrame.spriteFrame = spriteFrame;
            }
        } );
        // //载入角色头像
        if(this.mIcon){
            let role = main.player.role
            let roleIcon = main.sManage.getRoleIconById(role.icon)
            if(roleIcon){
                cc.resources.load('icon/role_head/'+roleIcon.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
                    if(err){
                        cc.error(err);
                    }else{
                       this.mIcon.spriteFrame = spriteFrame;
                    }
                } );
            }
        }
       
    }
    /**
     * 动态设置背景
     * @param main 
     */
    public flushFrame(main:Main){
        let player = main.player
        let roleFrame = main.sManage.getRoleIconById(player.role.frame)
        if(roleFrame){
            cc.resources.load('icon/role_head/txk/'+roleFrame.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
                if(err){
                    cc.error(err);
                }else{
                   this.mFrame.spriteFrame = spriteFrame;
                }
            } );
        }
    }

    public flush(main:Main){
        this.loadIcon(null)
        this.flushLock(main)
    }

    public flushLock(main:Main){
        let roleIcon = main.player.roleIcon
        if(roleIcon.open.indexOf(this.roleIcon.id) < 0){
            this.mLock.active = true
        }else{
            this.mLock.active = false
        }
    }

    public setCheck(bool){
        if(this.mCheck){
            this.mCheck.active = bool
        }
    }

}
