import { GameViewType } from "../../main/ViewManage";
import ViewObject from "../../main/ViewObject";
import CMath from "../../util/CMath";
import RoleFrameView from "./roleicon/RoleFrameView";
import RoleIconView from "./roleicon/RoleIconView";
import RoleTitleItem from "./roleicon/RoleTitleItem";
import RoleTitleView from "./roleicon/RoleTitleView";
import TapRoleName from "./TapRoleName";

/**
 * 角色信息
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class TapRole extends ViewObject {

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

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

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

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

    @property(cc.Prefab)
    mFrameIcon: cc.Prefab = null;

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

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

    onLoad () {
        let player = this.main.player;
        this.mName.string = player.role.name;
        this.mId.string = player.role.id;
        this.mZdl.string = ''+player.role.zdl
    }

    /**
     * 
     * @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);
        }
    }


    onEnable(){
        this.flush()
    }
    public flush(){
        let player = this.main.player;
        this.mName.string = player.role.name;
        
        let roleIcon = this.main.sManage.getRoleIconById(player.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;
                }
            } );
        }
        let roleFrame = this.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;
                }
            } );
        }

        this.flushTitle()

    }
    private flushTitle(){
        let roleIcon = this.main.player.roleIcon
        let open = roleIcon.open
        let nodes = this.mContentHas.children
        let copy:Array<number> = open.slice();
        for (let i = nodes.length - 1; i >= 0; i--) {
            const node = nodes[i];
            let rti = node.getComponent(RoleTitleItem)
            if(open.indexOf(rti.roleIcon.id) < 0){
                node.destroy()
            }
            for (let j = 0; j < copy.length; j++) {
                const tempId = copy[j];
                if(tempId == rti.roleIcon.id){
                    copy.splice(j,1);
                    break
                }
            }
        }
        let x0 = this.mContentHas.width/2 - 100
        let y0 = this.mContentHas.height/2 - 30
        for (let i = 0; i < copy.length; i++) {
            const id = copy[i];
            if(id >= 300){
                let node = cc.instantiate(this.mFrameIcon)
                node.x = CMath.getRandom(-x0,x0)
                node.y = CMath.getRandom(-y0,y0)
                let frameIcon = node.getComponent(RoleTitleItem)
                let roleIcon = this.main.sManage.getRoleIconById(id);
                frameIcon.roleIcon = roleIcon
                frameIcon.init()
                frameIcon.mLock.active = false
                node.parent = this.mContentHas
            }
        }
    }

    public onclickRename(){
        this.main.viewManage.loadFunc(GameViewType.tap_role_name,(viewObject:ViewObject)=>{
            let rename = viewObject as TapRoleName
            rename.tapRole = this
            viewObject.show(this);
        });
    }
    
    public onclickIcon(){
        this.main.viewManage.loadFunc(GameViewType.role_icon_view,(viewObject:ViewObject)=>{
            let rename = viewObject as RoleIconView
            rename.tapRole = this
            viewObject.show(this);
        });
    }
    public onclickFrame(){
        this.main.viewManage.loadFunc(GameViewType.role_frame_view,(viewObject:ViewObject)=>{
            let rename = viewObject as RoleFrameView
            rename.tapRole = this
            viewObject.show(this);
        });
    }
    public onclickTitle(){
        this.main.viewManage.loadFunc(GameViewType.role_title_view,(viewObject:ViewObject)=>{
            let rename = viewObject as RoleTitleView
            rename.init()
            viewObject.show(this);
        });
    }


}
