import { _decorator, Component, Node, ModelComponent, find, Prefab, loader, instantiate, Mesh, systemEvent, SystemEventType, Quat, Vec2, director, game, view, Touch, LabelComponent } from 'cc';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { DataModelEventType } from '../../../engines/models/DataModelEventType';
import { SceneManager } from '../../../engines/scenes/SceneManager';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
const { ccclass, property } = _decorator;

@ccclass('PrepareMediator')
export class PrepareMediator extends GUIMediator {
    
    @property({
        type:LabelComponent
    })
    glodLabel:LabelComponent=null;

    @property({
        type:LabelComponent
    })
    glodLabel1:LabelComponent=null;

    @property({
        type:LabelComponent
    })
    diamondLabel:LabelComponent=null;

    @property({
        type:LabelComponent
    })
    gunNameLabel:LabelComponent=null;


    private modelView:ModelComponent;

    private prefabInstance:Node;
    private prefabModelComponent:ModelComponent;
    OnShow(data?:any):void{
        super.OnShow(data);
        this.RefreshGunModel();
        this.RefreshGlod();
        this.RefreshDiamond();
        this.AddEvents();
    }

    /**
     * 初始化枪的模型
     */
    private RefreshGunModel():void{
        if(this.modelView==null){
            let modelNode:Node=find("Canvas/ModelView");
            this.modelView=modelNode.getComponent(ModelComponent);
        }
        this.modelView.node.active=true;

        let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
        let prefab:Prefab=loader.getRes(weaponConfig.prefab);
        this.prefabInstance=instantiate(prefab);
        this.prefabModelComponent=this.prefabInstance.getComponentInChildren(ModelComponent);
        this.modelView.mesh=this.prefabModelComponent.mesh;
        this.modelView.setMaterial(this.prefabModelComponent.getMaterial(0),0);

        if(this.gunNameLabel!=null){
            this.gunNameLabel.string=weaponConfig.name;
        }
    }

    OnHide():void{
        this.RemoveEvents();

        this.modelView.mesh=null;
        this.modelView.setMaterial(null,0);
        this.prefabInstance.destroy();

        this.modelView.node.active=false;
    }

    private AddEvents():void{
        systemEvent.on(cc.SystemEventType.TOUCH_START,this.TouchStartHandler,this);
        systemEvent.on(cc.SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
        systemEvent.on(cc.SystemEventType.TOUCH_MOVE,this.TouchEndHandler,this);
        GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
    }

    private RemoveEvents():void{
        systemEvent.off(SystemEventType.TOUCH_START,this.TouchStartHandler,this);
        systemEvent.off(SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
        systemEvent.off(SystemEventType.TOUCH_MOVE,this.TouchEndHandler,this);
        GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
    }

    private GameModelPropertyChanged(key:string):void{
        switch (key) {
            case GamePropertys.gold:
                this.CallNextFrame(this.RefreshGlod.bind(this));
                break;
            case GamePropertys.diamond:
                this.CallNextFrame(this.RefreshDiamond.bind(this));
                break;
            case GamePropertys.currentWeaponId:
                this.CallNextFrame(this.RefreshGunModel.bind(this));
                break;
        }
    }

    private RefreshGlod():void{
        if(this.glodLabel!=null){
            this.glodLabel.string=GameModel.single.gold.toString();
        }
    }

    private RefreshDiamond():void{
        if(this.diamondLabel!=null){
            this.diamondLabel.string=GameModel.single.diamond.toString();
        }
    }
    

    private startRotation:Quat=new Quat();
    private currentRotation:Quat=new Quat();
    private startPoint:Vec2=new Vec2();
    private currentPoint:Vec2=new Vec2();

    private TouchStartHandler(touch:Touch,touchEvent):void{
        let pos:Vec2=touch.getLocation();
        this.startPoint.set(pos.x,pos.y);
        this.startRotation.x=this.modelView.node.rotation.x;
        this.startRotation.set(this.modelView.node.rotation.x,this.modelView.node.rotation.y,this.modelView.node.rotation.z,this.modelView.node.rotation.w);


    }

    private TouchMoveHandler(touch,touchEvent):void{
        let pos:Vec2=touch.getLocation();
        this.currentPoint.set(pos.x,pos.y);
        let dis:number=Vec2.distance(this.currentPoint,this.startPoint);
        let value:number=dis/view.getCanvasSize().width;

        Quat.rotateY(this.currentRotation,this.startRotation,value);
        
        Quat.normalize(this.currentRotation,this.currentRotation);
        
        this.modelView.node.setRotation(this.currentRotation);
    }

    private TouchEndHandler(...arg):void{
        console.log("拖拽模型结束");
    }

    StartGame():void{
        this.HideSelf();
        SceneManager.single.Swicth("FightingScene");
    }
}