import i18n from "../../i18n/i18n";
import ViewObject from "../../main/ViewObject";
import { HttpStateType, ReveData } from "../../util/CHttp";
import TapRole from "./TapRole";

/**
 * 角色改名
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class TapRoleName extends ViewObject{

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

    @property(cc.EditBox)
    mEditBox: cc.EditBox = null;

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

    @property(cc.Node)
    mFreeNode: cc.Node = null;//免费

    @property(cc.Node)
    mGoldNode: cc.Node = null;

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

    public tapRole:TapRole = null;

    /**
     * 
     * @param prev 父界面
     */
     public show(prev?:ViewObject){
        if(prev){
            this.prev = prev;
            this.prev.__close();
        }
        this.main.viewManage.popView1(this.node);
        if(this.main && this.main.gameHttp){
            this.main.gameHttp.pushEvent(this);
        }
    }


    start () {
        let role = this.main.player.role
        this.mName.string = role.name
        this.mCount.string = i18n.t('第{value}次改名',{value:(role.renameCount+1)})

        if(role.renameCount <= 0){
            this.mFreeNode.active = true
            this.mGoldNode.active = false
        }else{
            this.mFreeNode.active = false
            this.mGoldNode.active = true
        }
    }

    public onclickRename(){
        
        let msg = {
            name:this.mEditBox.string
        }
        this.main.gameHttp.sendJson('set/v1/name',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.main.player.role.name = this.mEditBox.string
                    this.exitDistroy()
                    this.main.showTips('改名成功');
                    this.tapRole.flush()
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }

}
