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

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

@ccclass
export default class TitleInfoView extends cc.Component{

    @property(RoleTitleItem)
    mRoleIcon: RoleTitleItem = 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 roleTitleView:RoleTitleView = null
    private fip:RoleTitleItem = null

    public main:Main

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

        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.flushButton()

    }
    public initFrame(main:Main,fip:RoleTitleItem){
        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.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.roleIconRemove();
        }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.main.showTips('使用成功');
                    this.flushButton()
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }
    /**
     * 移除
     */
    private roleIconRemove(){
        let roleIcon = this.fip.roleIcon
        let msg = {
            id:roleIcon.id,
        }
        this.main.gameHttp.sendJson('set/v1/removeTitle',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    let open =  this.main.player.roleIcon.open
                    for (let i = 0; i < open.length; i++) {
                        const id = open[i];
                        if(id == roleIcon.id){
                            open.splice(i,1)
                            break
                        }
                    }
                    this.flushButton()
                    this.main.showTips('卸下成功');
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }
}
