import { _decorator, Color, Label, Node, Size, Sprite, SpriteFrame, tween, UITransform, VerticalTextAlignment } from 'cc';
import { BaseSprite } from '../../GlobalScript/CommonComponent/BaseSprite';
import { NpkImage } from '../../Tool/NPKImage';
import { BaseSpriteFrame } from '../../GlobalScript/CommonComponent/BaseSpriteFrame';
const { ccclass } = _decorator;

/// 玩家角色类型
export enum CharacterType{
    /// 剑魂
    JianHun = 0,

}


/// 玩家Node封装
@ccclass('Gamer')
export class GamerNode extends Node {

    /// 玩家角色类型
    private _characterType : CharacterType;

    /// 玩家名称
    private GamerNameLabel : Label;

    /// 幻想点数
    private _count: number = 0;
    private countLabel: Label;

    /// 玩家排序的精灵
    private serialNumberSprite: Sprite;
    private serialOneFrame: SpriteFrame;
    private serialTwoFrame: SpriteFrame;
    private serialThreeFrame: SpriteFrame;

    private blue = new Color('00baff');

    set charchterType(type:CharacterType){
        if(!this.getChildByName('GamerCharacterNode')){
            this._characterType = type;
            this.initCharacterNode();
        }
    }

    /// 设置新的幻想点数
    set count(newCount: number){

        let numberString = newCount.toLocaleString();

        const obj = {n:this._count};

        if (numberString != this.GamerNameLabel.string){
            tween(obj).to(1.0,{ n: newCount },{onUpdate:(target,ration)=>{
                this._count = obj.n;
                const count:number = parseInt(obj.n.toFixed(0));
                this.countLabel.string = count.toLocaleString();
                this.countLabel.color = this.getCountColor(obj.n);
            }},).start();
        }


    }

    /// 设置玩家名称
    set gamerName(newName: string){
        let name = newName;
        if (name.length > 6){
            name = newName.slice(0,3) + '...' + newName.slice(newName.length - 3);
        }
        this.GamerNameLabel.string = name;
    }

    /// 玩家排序
    set serialNumber( serial: 1 | 2 | 3){
        switch (serial) {
            case 1:
                this.serialNumberSprite.spriteFrame = this.serialOneFrame;
                this.GamerNameLabel.color = Color.RED;
                break;
            case 2:
                this.serialNumberSprite.spriteFrame = this.serialTwoFrame;
                this.GamerNameLabel.color = this.blue;
                break;
            case 3:
                this.serialNumberSprite.spriteFrame = this.serialThreeFrame;
                this.GamerNameLabel.color = Color.GREEN;
                break;
        }
    }

    private getCountColor(count:number):Color{
        if ( count < 50000){
            return Color.RED;
        }else if ( count < 200000){
            return Color.YELLOW;
        }else{
            return this.blue;
        }
    }


    constructor(gamerName:string){
        super();

        this.initBackgroundNode();
        this.initPNode();
        this.initCountNode();
        this.initGamerNameNode();
        this.initSerialNumberNode();

        this.gamerName = gamerName;

        /// 提前加载精灵帧
        new BaseSpriteFrame(NpkImage.ingame, 1, _SpriteFrame => { this.serialOneFrame = _SpriteFrame });
        new BaseSpriteFrame(NpkImage.ingame, 2, _SpriteFrame => { this.serialTwoFrame = _SpriteFrame });
        new BaseSpriteFrame(NpkImage.ingame, 3, _SpriteFrame => { this.serialThreeFrame = _SpriteFrame });
    }

    /// 背景
    initBackgroundNode(){
        /// 背景节点
        const backgroundNode = new Node('GamerBackgroundNode');
        this.addChild(backgroundNode);

        /// 给背景节点添加 baseSprite 组件
        const backgroundComponent = backgroundNode.addComponent( BaseSprite );
        backgroundComponent.updateSpriteFrame(NpkImage.ingame,32);

    }

    /// 角色
    initCharacterNode(){
        const node = new Node('GamerCharacterNode');
        this.addChild(node);

        /// 添加 baseSprite 组件
        const backgroundComponent = node.addComponent( BaseSprite );
        backgroundComponent.updateSpriteFrame(NpkImage.character_sd,this._characterType);
    }

    /// P图标
    initPNode(){
        const node = new Node('GamerPNode');
        this.addChild(node);

        node.setPosition(50,-9.5);

        /// 添加 baseSprite 组件
        const backgroundComponent = node.addComponent( BaseSprite );
        backgroundComponent.updateSpriteFrame(NpkImage.ingame,53);

    }

    /// 幻想点数
    initCountNode(){
        const node = new Node('GamerCountNode');
        this.addChild(node);

        node.setPosition(100,-18);
        const trf = node.addComponent(UITransform);
        trf.contentSize = new Size(60,20);

        const label = node.addComponent(Label);
        label.fontSize = 14;
        label.string = '0';
        label.color = Color.RED;
        label.verticalAlign = VerticalTextAlignment.CENTER;
        this.countLabel = label;


    }

    /// 玩家名称
    initGamerNameNode(){
        const node = new Node('GamerNameNode');
        this.addChild(node);

        node.setPosition(90,-40);
        const trf = node.addComponent(UITransform);
        trf.contentSize = new Size(100,20);

        const label = node.addComponent(Label);
        label.string = '';
        label.fontSize = 12;
        label.color = Color.GRAY;
        label.verticalAlign = VerticalTextAlignment.CENTER;

        this.GamerNameLabel = label;
    }

    /// 玩家顺序
    initSerialNumberNode(){
        const node = new Node('GamerNameNode');
        this.addChild(node);

        node.setPosition(160,-30);

        this.serialNumberSprite = node.addComponent( Sprite );

    }



}


