import i18n from "../../../i18n/i18n";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import FFCalAttr from "../../data/FFCalAttr";
import { __RoleIcon } from "../../data/sdata/SManage";
import RoleIconItem from "./RoleIconItem";
import RoleFrameView from "./RoleFrameView";
import RoleIconView from "./RoleIconView";
import Mail from "../../mail/Mail";
import Main from "../../../main/Main";

/**
 * 头像详情查看
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class IconInfoView extends cc.Component {

    @property(RoleIconItem)
    mRoleIcon: RoleIconItem = null;

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

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

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

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

    @property(cc.Label)
    mSp: cc.Label = null;
 
    @property(cc.Sprite)
    mButtonIcon: cc.Sprite = null;

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

    @property([cc.SpriteFrame])
    mButtonIconFrame: Array<cc.SpriteFrame> = [];

    public roleIconView:RoleIconView = null
    public roleFrameView:RoleFrameView = null
    private fip:RoleIconItem = null

    public openType = 0;//1:头像,2:框

    public main:Main

    public init(main:Main,fip:RoleIconItem){
        this.main = main
        this.fip = fip
        let roleIcon = fip.roleIcon
        this.mRoleIcon.roleIcon = roleIcon

        this.mAtk.string = ''+roleIcon.atk
        this.mDef.string = ''+roleIcon.def
        this.mHp.string = ''+roleIcon.hp
        this.mSp.string = ''+roleIcon.sp

        let zdl = FFCalAttr.getZdl(roleIcon)
        this.mZdl.string = ''+zdl
        this.mRoleIcon.loadIcon(this.main)
        this.flushButton()

    }
    public initFrame(main:Main,fip:RoleIconItem){
        this.main = main
        this.fip = fip
        let roleIcon = fip.roleIcon
        this.mRoleIcon.roleIcon = roleIcon

        this.mAtk.string = ''+roleIcon.atk
        this.mDef.string = ''+roleIcon.def
        this.mHp.string = ''+roleIcon.hp
        this.mSp.string = ''+roleIcon.sp

        let zdl = FFCalAttr.getZdl(roleIcon)
        this.mZdl.string = ''+zdl
        this.mRoleIcon.loadFrame(this.main)
        this.flushButton()
    }
    private flushButton(){
        let status = this.getStatus()
        if(status == 0){//还未获得
            this.mButtonIcon.spriteFrame = this.mButtonIconFrame[2]
            this.mButtonLabel.string = i18n.t('未获得')
        }else if(status == 1){//已经激活
            this.mButtonIcon.spriteFrame = this.mButtonIconFrame[0]
            this.mButtonLabel.string = i18n.t('使用')
        }else if(status == 2){//未激活
            this.mButtonIcon.spriteFrame = this.mButtonIconFrame[1]
            this.mButtonLabel.string = i18n.t('激活')
        }
    }

    private getStatus():number{
        let roleIcon = this.fip.roleIcon
        let roleIcons = this.main.player.roleIcon
        if(roleIcons.open.indexOf(roleIcon.id) >= 0){
            return 1
        }if(roleIcons.data.indexOf(roleIcon.id) >= 0){
            return 2
        }
        return 0;
    }

    public onclick(){
        let status = this.getStatus()
        if(status == 1){
            this.roleIconUser()
        }else if(status == 2){
            this.roleIconActive()
        }
    }
    /**
     * 激活
     */
    private roleIconActive(){
        let roleIcon = this.fip.roleIcon
        let msg = {
            id:roleIcon.id,
        }
        this.main.gameHttp.sendJson('set/v1/activeIcon',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.main.player.roleIcon.open.push(roleIcon.id)
                    this.flushButton()
                    this.fip.flush(this.main)
                    if(this.roleIconView){
                        this.roleIconView.flush(this.fip)
                    }
                    if(this.roleFrameView){
                        this.roleFrameView.flush(this.fip)
                    }
                    this.main.showTips('激活成功');
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }
    /**
     * 使用
     */
    private roleIconUser(){
        let roleIcon = this.fip.roleIcon
        let msg = {
            type:this.openType,
            id:roleIcon.id,
        }
        this.main.gameHttp.sendJson('set/v1/useIcon',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    if(this.openType == 1){
                        this.main.player.role.icon = roleIcon.id
                    }else{
                        this.main.player.role.frame = roleIcon.id
                    }
                    
                    this.flushButton()
                    this.fip.flush(this.main)
                    if(this.roleIconView){
                        this.roleIconView.flush(this.fip)
                    }
                    if(this.roleFrameView){
                        this.roleFrameView.flush(this.fip)
                    }
                    this.main.showTips('使用成功');
                    this.main.home.flush()
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }

}
