import { resources } from "cc";
import { BehaviorManager } from "../../extensions/Behavior Creator/runtime/main";
import SystemBase from "../../extensions/ngame/assets/ngame/system/SystemBase";
import JNSocket from "../../extensions/ngame/assets/ngame/network/JNSocket";
import { JNSyncAction } from "../../extensions/ngame/assets/ngame/sync/JNSyncAction";
import { JNSyncMessage } from "../../extensions/ngame/assets/ngame/sync/JNSyncMessage";
import JNSyncFrame, { JNFrameInfo, JNFrameInfos, JNFrameInputs } from "../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
import JNSyncFrameComponent from "../../extensions/ngame/assets/ngame/sync/frame/game/JNSyncFrameComponent";
import JNSyncFrameProtoComponent from "../../extensions/ngame/assets/ngame/sync/frame/game/JNSyncFrameProtoComponent";
import JNLayer, { JNLayerAnim, JNLayerInfo } from "../../extensions/ngame/assets/ngame/ui/JNLayer";
import JNLayerBase from "../../extensions/ngame/assets/ngame/ui/base/JNLayerBase";
import { EventDispatcher } from "../../extensions/ngame/assets/ngame/util/EventDispatcher";
import { JsonLoad, JsonUtil } from "../../extensions/ngame/assets/ngame/util/JsonUtil";
import NGameMessage from "../../extensions/ngame/assets/ngame/util/NGameMessage";
import GBattleModeManager from "./battle/GBattleModeManager";
import { GLayer, GUI, UIConfig } from "./ui/UIConfig";
import JLoaderBundle from "../../extensions/ngame/assets/ngame/system/JLoaderBundle";
import { sp } from "cc";
import { SpriteFrame } from "cc";
import Loading from "../../extensions/ngame/assets/ngame/util/Loading";
import { TB, Tables } from "./config/data/schema";
import { JsonAsset } from "cc";
import { GAction } from "./consts/GAction";
import { StorageData, StorageEnum } from "./consts/GData";
import { JAPI, JAPIConfig } from "../../extensions/ngame/assets/ngame/util/JAPI";
import { AppData } from "./AppData";
import AppAction from "./AppAction";
import { Asset } from "cc";
import { Component } from "cc";

let APIPath = `http://localhost:8080`
let WsPath = `ws://localhost:8080/websocket`
// let APIPath = `http://192.168.1.23:8080`
// let WsPath = `ws://192.168.1.23:8080/websocket`
// let APIPath = `http://192.168.0.118:8080`
// let WsPath = `ws://192.168.0.118:8080/websocket`
// let APIPath = `https://api.pet.jisol.cn`
// let WsPath = `wss://api.pet.jisol.cn/websocket`

//重写UI
class JNGLayer extends JNLayer{
    layers: string[] = [GLayer.View,GLayer.Popup,GLayer.Tips];
    views: { [key: string]: JNLayerInfo; } = UIConfig;
}

//重写Socket
class JNGSocket extends JNSocket{
    async onInit() {
        this.on(GAction.TOKEN_EXPIRED,this.onTokenExpired.bind(this));
        await super.onInit();
    }
    public async url():Promise<string> {
        return new Promise<string>(resolve => {
            //获取Token
            let token = StorageData.get(StorageEnum.Token);
            if(token){
                resolve(`${WsPath}/${token}`)
                return;
            }

            const loginResolve = (token:string) => {
                resolve(`${WsPath}/${token}`);
            }
            //如果没有Token则弹出登入页面
            app.layer.Open(GUI.Login,loginResolve);
        });
    }
    //Token失效
    onTokenExpired(){
        console.log("onTokenExpired");
        //清除Token
        StorageData.delect(StorageEnum.Token);
    }
}

// 重写帧同步
class JNGSyncFrame extends JNSyncFrame{

    protected onResetValue(){
        //重置状态机
        BehaviorManager.deleteInstance();
        super.onResetValue();
    }

    async onServerData(start?:number,end?:number): Promise<JNFrameInfos> {

        return app.proto
                .getType(JNSyncMessage.JNFrameInfos)
                .decode(
                    new Uint8Array(

                        (await app.api.get("/sync/frame",{
                            responseType:'arraybuffer',
                            params:{
                                start,
                                end
                            }
                        })).data
                    )
                ) as any;

    }

    onSendInput(message: JNFrameInputs) {
        app.socket.Send(JNSyncAction.NSyncFrameInput,message,JNSyncMessage.JNFrameInputs);
    }

}


export var TD:Tables = null;
//读写config
export class JNGConfig extends SystemBase{

    static loading = "JNGConfig";
    
    async onInit(): Promise<any> {

        app.loading.setCurrent(JNGConfig.loading);

        let json2 = resources.getDirWithPath("config/json/",JsonAsset).map((info) => {
            let args = info.path.split("/");
            return {
                name:args[args.length - 1],
                bundle:"resources",
                path:"config/json/"
            }
        })

        for (const key in json2) {
            const load = json2[key];
            await JsonUtil.load(load);
        }

        TD = new Tables((file) => JsonUtil.get(file))
        console.log(TD);

        app.loading.ok(JNGConfig.loading);
    }

}

//资源初始器
export class JLoaderBattle extends JLoaderBundle{

    static loading = "JLoaderBattle";
    static loadingInit = "JLoaderBattle_Init";

    //资源
    resources:{[id:number]:Asset} = {};
    battleResources:number[] = [];     //战斗资源Id

    async onInit(): Promise<any> {

        app.loading.setCurrent(JLoaderBattle.loading);

        await super.onInit();

        app.loading.ok(JLoaderBattle.loading);

        app.loading.setCurrent(JLoaderBattle.loadingInit);

        //默认加载全部资源
        await this.loadBattleResources(...TD.TbBattleResource.getDataList());

        app.loading.ok(JLoaderBattle.loadingInit);
        
    }

    //加载战斗资源
    async loadBattleResources(...ress:TB.TbBattleResource[]){

        for (const res of ress) {
            await this.loadBattleResource(res);
        }

    }
    //加载战斗资源
    loadBattleResource(res:TB.TbBattleResource){
        return (new Promise<void>(r => {

            let Type:new ()=>Asset = [sp.SkeletonData,SpriteFrame][res.type];

            this.bundle.load(res.path,Type,(error,data) => {
                if(this.battleResources.indexOf(res.id) < 0){
                    //添加永久资源
                    this.resources[res.id] = data;
                    data.addRef();
                }
                r();
            })
        }))
    }

    //加载组件资源
    async loadComponentResource<T extends Asset>(res:TB.TbBattleResource,comp:Component):Promise<T>{
        return (new Promise<T>(r => {
            this.bundle.load(res.path,(error,data) => {
                if(!this.resources[res.id])
                    this.resources[res.id] = data;
                //添加资源次数
                data.addRef();
                //添加销毁资源引用
                let onDestroy = comp["onDestroy"];
                comp["onDestroy"] = () => {
                    if(onDestroy)
                        onDestroy.bind(comp)();
                    data.decRef();
                    this.onUpdateResources();
                }
                r(data as T);
            })
        }))
    }

    //销毁长久资源
    clearBattleResources(...ress:TB.TbBattleResource[]){
        this.battleResources.forEach(id => {
            if(this.resources[id]){
                this.resources[id].decRef();
            }
        });
        this.battleResources = [];
        this.onUpdateResources();
    }

    //更新资源(过滤没有用的资源)
    onUpdateResources(){
        for (const key in this.resources) {
            if (Object.prototype.hasOwnProperty.call(this.resources, key)) {
                const data = this.resources[key];
                if(!data.isValid) this.resources[key] = null;
            }
        }
    }

    //获取资源
    getData<T extends Asset>(resId:number | string):T{

        let res = this.resources[resId]
        if(!res) console.info(`[JLoaderBattle] 未加载资源${resId}`);
        
        return res as T;
        
    }

    //获取角色Spine
    getRoleSpine(roleId:number){
        return this.getData<sp.SkeletonData>(TD.TbGRole.get(roleId).spine)
    }

    //获取地图
    getMap(mapId:number):SpriteFrame[]{
        let info = TD.TbGMap.get(mapId)
        return ["1","2","3"].map(key => {
            return this.getData<SpriteFrame>(info[`map${key}`]);
        })
    }

}

export const app = {
    layer : new JNGLayer(), //UI
    socket : new JNGSocket(), //Socket
    sync : new JNGSyncFrame(), //同步
    event : EventDispatcher.getIns(), //通知
    proto : NGameMessage.getIns(), //消息
    api : JAPI.create({
        baseURL: `${APIPath}`,
    }), //请求
    // api : {}, //请求
    battle : GBattleModeManager.getIns(), //战斗
    config : new JNGConfig(), //配置文件
    battleRes : new JLoaderBattle("battle"), //battle包
    tbRes : new JLoaderBundle("tbresource"), //tbresource包
    res : new JLoaderBundle("resources"), //resources包
    data : new AppData(), //游戏基础信息
    action : new AppAction(), //游戏行为
    loading : new Loading({
        [JNGConfig.loading]:{title:"加载配置文件"},
        [JLoaderBattle.loading]:{title:"加载战斗资源"},
        [JLoaderBattle.loadingInit]:{title:"初始化战斗资源"},
        [AppData.loading]:{title:"初始化信息"},
    })
}

app.api.addRequestInterceptors((config:JAPIConfig) => {
    //设置Token
    if(StorageData.get(StorageEnum.Token))
        config.headers["Token"] = StorageData.get(StorageEnum.Token);
    return true;
})