import {PanelBaseControl} from "../Control/PanelBaseControl";
import {GameStateControl} from "../Control/GameStateControl";
import { DispatcherManager } from "../utils/DispatcherManager";
import {GameState} from "../utils/GameState";
import {PropManager} from "../PrefabControl/PropManager";
import {AgencyUserMessage} from "../Agency/AgencyUserMessage";
import {RoleData} from "../Data/RoleData";
import {UserInfoEvent} from "../Data/UserInfoEvent";
import {AgencySkin} from "../Agency/AgencySkin";
import {AgencyInGame} from "../Agency/AgencyInGame";
import { GlobalTip } from "../utils/GlobalTip";
import { ViewManager } from "../View/ViewManager";
import {AgencyLottery} from "../Agency/AgencyLottery";
import ChoutiPanView from "../Push/ChoutiPanView";
import TuijianView from "../Push/TuijianView";
import { FYHD_Sdk } from "../WXSDK/FYHD_Sdk";
import { ttad } from "../WXSDK/ttad";

export class MainPanel extends PanelBaseControl
{
    static key:string = "MainPanel";

    c_state:fairygui.Controller;
    load_clickpanel:fairygui.GLoader;
    load_start:fairygui.GLoader;
    txt_lv:fairygui.GTextField;
    pro_dis:fairygui.GProgressBar;
    txt_mingci:fairygui.GTextField;
    txt_gold:fairygui.GTextField;
    SkinBtn:fairygui.GButton;
    MoreGameBtn:fairygui.GButton;
    ExitBtn:fairygui.GButton;
    LotteryBtn:fairygui.GButton;
    SetBtn:fairygui.GButton;

    MoreFunBtnScale:fairygui.Transition;

    //更多好玩按钮
    MoreFunBtn:fairygui.GButton;

    //皮肤按钮动效
    SkinAni:fairygui.Transition;
    //更多好玩按钮动效
    MoreAni:fairygui.Transition;
    //抽奖按钮动效
    LotteryAni:fairygui.Transition;

    private agencyusermessage:AgencyUserMessage;

    constructor() 
    { 
        super("main","mainview"); 
    }

    private updateUserDataHandler():void
    {
        this.txt_gold.text = this.agencyusermessage.RoleData.Money.toString();
    }

    private returnmainHandler():void
    {
        var RoleData:RoleData = this.agencyusermessage.RoleData;
        // if( RoleData.nowlevel <= 2 )
        // {
        //     this.c_state.selectedIndex = 0;
        // }
        // else
        // {
            this.c_state.selectedIndex = 1;
        // }
    }

    protected onInit():void
    {
        super.onInit();
    }

    /****************************************** */
     // 一只凤凰的分辨率是550 * 400
     private phoenixWidth: number = 550;
     private phoenixHeight: number = 400;

     private bgColorTweener:Laya.Tween = new Laya.Tween();
     private gradientInterval: number = 2000;
     private bgColorChannels: any = { r: 99, g: 0, b: 0xFF };
    private createPhoenixes(): void {
        var scaleFactor: number = Math.min(
            Laya.stage.width / (this.phoenixWidth * 2),
            Laya.stage.height / this.phoenixHeight);

        // 加了混合模式的凤凰
        var blendedPhoenix:Laya.Animation 
        blendedPhoenix.blendMode = "lighter";
        blendedPhoenix.scale(scaleFactor, scaleFactor);
        blendedPhoenix.y = (Laya.stage.height - this.phoenixHeight * scaleFactor) / 2;

        // 正常模式的凤凰
        var normalPhoenix:Laya.Animation;
        normalPhoenix.scale(scaleFactor, scaleFactor);
        normalPhoenix.x = this.phoenixWidth * scaleFactor;
        normalPhoenix.y = (Laya.stage.height - this.phoenixHeight * scaleFactor) / 2;
    }
/****************************************** */
    

    protected initView():void
    {
        this.ExitBtn.visible = false;
        this.MoreFunBtn.visible = false;
        this.MoreGameBtn.visible = false;
        ttad.Showbanner();
        // Laya.timer.once(500,this,()=>{
        //     console.log("main界面白名单----",FYHD_Sdk.iswhite);
        //     if(FYHD_Sdk.iswhite){
        //         this.ExitBtn.visible = true;
        //     }
        //     else{
        //         this.ExitBtn.visible = false;
        //     }
        // })
        this.MoreFunBtnScale = this.getTransitionByName("morefunbtnscale");
        
        Laya.timer.loop(1000,this,this.playanimator2);
        this.SkinAni = this.SkinBtn.getTransition("skinani");
        this.LotteryAni = this.LotteryBtn.getTransition("lotteryani");
        this.MoreAni = this.MoreGameBtn.getTransition("moreani");
        Laya.timer.loop(3000,this,this.playanimator);

        this.agencyusermessage = this.getProxy( AgencyUserMessage );
        PropManager.pro_jindu = this.pro_dis;
        PropManager.txt_mingci = this.txt_mingci;
        this.c_state = this.getControllerByBame( "c_state" );
        GameStateControl.Instance.setClickPanel( this.load_clickpanel );
        this.returnmainHandler();
        this.updateUserDataHandler();
    }
    playanimator(){
        this.SkinAni.play(null,1);
        this.LotteryAni.play(null,1);
        this.MoreAni.play(null,1);
    }
    playanimator2(){
        this.MoreFunBtnScale.play(null,1);
    }

    private choujiangclick():void
    {
        ttad.Clear();
        ( this.getProxy( AgencyLottery ) as AgencyLottery ).openView();
    }

    private tuijianclick():void
    {
        ViewManager.Instance.add( TuijianView );
    }

    private skinclick():void
    {
        ttad.Clear();
        ( this.getProxy( AgencySkin ) as AgencySkin ).openview();
    }

    private moreclick():void
    {
        // ViewManager.Instance.add( ChoutiPanView );
        ttad.CreateMoreGameBtn();
    }

    private btnclick():void
    {
        ttad.Clear();
        this.c_state.selectedIndex = 2;
        this.txt_lv.text = this.agencyusermessage.RoleData.getNowServerLevel().toString();
        ( this.getProxy( AgencyInGame ) as AgencyInGame ).startGame();
    }

    private setclick():void{
        
    }

    /*************************************** */
    private tiledMap:Laya.TiledMap;
    private layer:Laya.MapLayer;
    private sprite:Laya.Sprite;
    private mapLoaded(): void {
        this.layer = this.tiledMap.getLayerByIndex(0);

        var radiusX: number = 32;
        var radiusY: number = Math.tan(180 / Math.PI * 30) * radiusX;
        var color: string = "#FF7F50";
        this.sprite = new Laya.Sprite();
        this.sprite.graphics.drawLine(0, 0, -radiusX, radiusY, color);
        this.sprite.graphics.drawLine(0, 0, radiusX, radiusY, color);
        this.sprite.graphics.drawLine(-radiusX, radiusY, 0, radiusY * 2, color);
        this.sprite.graphics.drawLine(radiusX, radiusY, 0, radiusY * 2, color);
        Laya.stage.addChild(this.sprite);
    }
    /*************************************** */

    protected initEvent():void
    {
        this.AddEventByType( this.load_start,Laya.Event.CLICK,this,this.btnclick );
        this.AddEventByType( this.SkinBtn,Laya.Event.CLICK,this,this.skinclick );
        // this.AddEventByType( this.MoreGameBtn,Laya.Event.CLICK,this,this.moreclick );
        // this.AddEventByType( this.MoreFunBtn,Laya.Event.CLICK,this,this.moreclick );
        // this.AddEventByType( this.ExitBtn,Laya.Event.CLICK,this,this.tuijianclick );
        this.addDataEvent( GameState.returnMainView,this,this.returnmainHandler );
        this.AddEventByType( this.LotteryBtn,Laya.Event.CLICK,this,this.choujiangclick );
        this.addDataEvent( UserInfoEvent.UserInfoEvent_updateUserData,this,this.updateUserDataHandler );
    }

    

}