import {ResourceControl} from "./ResourceControl";
import {ResDataControl} from "./ResDataControl";
import {SkinItem} from "../utils/SkinItem";
import CpaDownsView from "../Push/CpaDownsView";
import CpaItem from "../Push/CpaItem";
import BaoKuanGameView from "../Push/BaoKuanGameView";
import ShiwantuijianView from "../Push/ShiwantuijianView";
import FriendInviteView from "../Push/FriendInviteView";
import TuijianView from "../Push/TuijianView";
import GiftView from "../View/GiftView";


/*
* name;
*/
export class ConnectControl extends Laya.EventDispatcher
{
    constructor() {
        super();
    }

    /**
     * 组件绑定
     */
    public static ConnectComponent(): void 
    {
        //main
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2pwlk1a", SkinItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c527", CpaDownsView);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c528", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c529", BaoKuanGameView);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c52a", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c52b", ShiwantuijianView);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c52c", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2eksc21", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2eksc22", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c52e", CpaItem);
        fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2uotu6h", CpaItem);
        // fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2ip616l", GiftView);
        // fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2k8c52d", TuijianView);
        // fairygui.UIObjectFactory.setPackageItemExtension("ui://lecxynh2kalr6k",FriendInviteView);
    }

    /****************************************************** */
    private motionCross:Boolean = false;
	private blendType:number = 0;
	private motionIndex:number = 0;
	private motions:Array<string> = ["run", "run_2", "attack", "attack_1", "attack_2", "dead", "idle_2", "idle_3", "idle_4", "idle4", "reload", "replace", "replace_2", "stop"];
	
    private sceneLoaded(scene:Laya.Scene3D):void {
        
        Laya.stage.addChild(scene);
        var animator:Laya.Animator = scene.getChildAt(2).getComponent(Laya.Animator);
        
        this.addButton(100, 100, 160, 30, "动画过渡:否", 20, function(e:Laya.Event):void {
            this.motionCross = !this.motionCross;
            if (this.motionCross)
                (e.target as Laya.Button).label = "动画过渡:是";
            else
                (e.target as Laya.Button).label = "动画过渡:否";
        });
        
        this.addButton(100, 160, 160, 30, "混合模式:全身", 20, function(e:Laya.Event):void {
            this.blendType++;   
            (this.blendType === 3) && (this.blendType = 0);
            switch (this.blendType) {
            case 0: 
                (e.target as Laya.Button).label = "混合模式:全身";
                break;
            case 1: 
                (e.target as Laya.Button).label = "混合模式:上身";
                break;
            case 2: 
                (e.target as Laya.Button).label = "混合模式:下身";
                break;
            }
        });
        
        this.addButton(100, 220, 260, 40, "切换动作:attack_2", 28, function(e:Laya.Event):void {
            switch (this.blendType) {
            case 0: 
                if (this.motionCross) {
                    animator.crossFade(this.motions[this.motionIndex], 0.2, 0);
                    animator.crossFade(this.motions[this.motionIndex], 0.2, 1);
                } else {
                    animator.play(this.motions[this.motionIndex], 0);
                    animator.play(this.motions[this.motionIndex], 1);
                }
                break;
            case 1: 
                if (this.motionCross)
                    animator.crossFade(this.motions[this.motionIndex], 0.2, 0);
                else
                    animator.play(this.motions[this.motionIndex], 0);
                break;
            case 2: 
                if (this.motionCross)
                    animator.crossFade(this.motions[this.motionIndex], 0.2, 1);
                else
                    animator.play(this.motions[this.motionIndex], 1);
                break;
            }
            (e.target as Laya.Button).label = "切换动作:" + this.motions[this.motionIndex];
            this.motionIndex++;
            (this.motionIndex === this.motions.length) && (this.motionIndex = 0);
        });
    }
    
    private addButton(x:number, y:number, width:number, height:number, text:string, size:number, clickFun:Function):void {
        var thiss = this;
        Laya.loader.load(["res/threeDimen/ui/button.png"], Laya.Handler.create(null, function():void {
        var changeActionButton:Laya.Button = Laya.stage.addChild(new Laya.Button("res/threeDimen/ui/button.png", text)) as Laya.Button;
        changeActionButton.size(width, height);
        changeActionButton.labelBold = true;
        changeActionButton.labelSize = size;
        changeActionButton.sizeGrid = "4,4,4,4";
        changeActionButton.scale(Laya.Browser.pixelRatio, Laya.Browser.pixelRatio);
        changeActionButton.pos(x, y);
        changeActionButton.on(Laya.Event.CLICK, thiss, clickFun);
        }));
    }
    /****************************************************** */

    
    /**
     * 数据绑定
     */
    public static Connnect(): void {
        this.ConnectFairygui();
        this.ConnectComponent();
    }

    /**
     * ui框架的绑定
     */
    public static ConnectFairygui(): void {
        fairygui.UIConfig.gameLoaderManager = ResourceControl.Instance;
        //特效绑定( url 绑定组名，只有公共特效需要绑定 ) "ui://fqpgxr1kl6mw0":"effect1"
        fairygui.UIConfig.gameEffectDict = {};
    }

    
}