import {PanelChildControl} from "../Control/PanelChildControl";
import {SkinData} from "../Data/SkinData";
import {AgencySkin} from "../Agency/AgencySkin";
import {PanelResDic} from "../PrefabControl/PanelResDic";
import { GlobalTip } from "../utils/GlobalTip";
import { AssetPath } from "../Config/AssetPath";
import {pingtaiutils} from "../WXSDK/pingtaiutils";
import MovieClip = Laya.MovieClip;
import { ttad } from "../WXSDK/ttad";
export class SkinItem extends fairygui.GButton 
{
    load_icon:fairygui.GLoader;
    c_state:fairygui.Controller;
    txt_need:fairygui.GTextField;
    pifuvo:SkinData;
    btn_0:fairygui.GLoader;
    private agencyskin:AgencySkin;
    constructor() 
    { 
        super(); 
    }

    /********************************************************** */
    private bgColorChannels: any = { r: 99, g: 0, b: 0xFF };
    private getHexColorString(): string {
        this.bgColorChannels.r = Math.floor(this.bgColorChannels.r);
        // 绿色通道使用0
        this.bgColorChannels.g = 0;
        //obj.g = Math.floor(obj.g);
        this.bgColorChannels.b = Math.floor(this.bgColorChannels.b);

        var r: String = this.bgColorChannels.r.toString(16);
        r = r.length == 2 ? r : "0" + r;
        var g: String = this.bgColorChannels.g.toString(16);
        g = g.length == 2 ? g : "0" + g;
        var b: String = this.bgColorChannels.b.toString(16);
        b = b.length == 2 ? b : "0" + b;
        return "#" + r + g + b;
    }
    /********************************************************** */

    protected constructFromXML(xml: any): void {
        super.constructFromXML(xml);
        for (var str in this._children) {
            var item: fairygui.GObject = this._children[str];
            this[item.name] = item;
        }
        this.onInit();
    }

    protected onInit():void
    {
        this.initView();
        this.initEvent();
    }

    private initView():void
    {
        this.agencyskin = PanelResDic.Instance.getSingleTon( AgencySkin );
        this.c_state = this.getController( "c_state" );
    }

    private initEvent():void
    {
        this.on( Laya.Event.CLICK,this,this.btnclick );
    }

    private btnclick():void
    {
        if( this.pifuvo )
        {
            switch( this.c_state.selectedIndex )
            {
                case 0:
                break;
                case 1: //拥有
                    this.agencyskin.GainPifu( this.pifuvo );
                break;
                case 2://活动获取
                    GlobalTip.Instance.showWordTips("暂未开放");
                break;
                case 3://看视频获取
                this.playvideocallback();
                    // pingtaiutils.Instance.playVideo( "",this,this.playvideocallback );
                break;
                case 4://金币购买
                    this.agencyskin.GainPifu( this.pifuvo );
                break;
            }
        }
    }

    /********************************************** */
    private SWFPath: string = "res/swf/dragon.swf";

		private MCWidth: number = 318;
		private MCHeight: number = 406;
    private createMovieClip(): void {
        var mc: MovieClip = new MovieClip();
        mc.load(this.SWFPath);

        mc.x = (Laya.stage.width - this.MCWidth) / 2;
        mc.y = (Laya.stage.height - this.MCHeight) / 2;

        Laya.stage.addChild(mc);
    }
    /********************************************** */

    private playvideocallback():void
    {
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                this.agencyskin.GainPifu( this.pifuvo );
            }
        })) 
    }

    public showData( vo:SkinData ):void
    {
        this.pifuvo = vo;
        if( this.pifuvo )
        {
            this.load_icon.icon = AssetPath.getroleicon( this.pifuvo.roleCFG.icon );
            if( this.agencyskin.RoleData.CharacterID == this.pifuvo.roleCFG.id )
            {
                this.c_state.selectedIndex = 0;
            }
            else if( this.pifuvo.state == 1 )
            {
                this.c_state.selectedIndex = 1;
            }
            else
            {
                switch( this.pifuvo.roleCFG.getState )
                {
                    case 1:
                        this.c_state.selectedIndex = 3;
                        this.txt_need.text = this.pifuvo.hasnum + "/" + this.pifuvo.roleCFG.need;
                    break;
                    case 2:
                        this.c_state.selectedIndex = 4;
                        this.txt_need.text = this.pifuvo.roleCFG.need.toString();
                    break;
                    case 3:
                        this.c_state.selectedIndex = 2;
                    break;
                }
            }
        }
    }

    /******************************************************** */
    private preFixNumber(num:number, strLen:number):string
    {
        return ("0000000000" + num).slice(-strLen);
    }

    private evalBgColor(): void {
        var color: number = Math.random() * 0xFFFFFF;
        var channels: Array<number> = this.getColorChannals(color);
    }

    private getColorChannals(color: number): Array<number> {
        var result: Array<number> = [];
        result.push(color >> 16);
        result.push(color >> 8 & 0xFF);
        result.push(color & 0xFF);
        return result;
    }

     /******************************************************** */

    public dispose():void
    {
        this.off( Laya.Event.CLICK,this,this.btnclick );
        super.dispose();
    }
    
}