
import GameDataCenter from "../../GameDataCenter";
import Load from "../../../utils/Load";
import { gameMethod } from "../../../common/gameMethod";
import { UserSex } from "../../const/TypeConst";

import { UserModelData } from "./UserModelData";
import EventMng from "../../../manager/EventMng";
import { NetworkEvent, UserEvent } from "../../const/EventConst";
import UIHelp from "../../../logic/ui/UIHelp";
import { ResEpsInfo } from "../../../shared/eps/PtlEpsInfo";
import { MsgEpsChange_s } from "../../../shared/eps/server/MsgEpsChange_s";
import { ResUserLogin } from "../../../shared/user/PtlUserLogin";

export default class UserModel extends UserModelData {

    taskNodes: { [key: string]: cc.Node } = {} //临时 主线任务指引节点

    private roleSexType: UserSex = UserSex.man; // 角色的性别类型
    selectHeadId: string = "";// 当前选中的头像
    selectChenghaoId: string = "";// 当前选中的称号
    newGetHead: string[] = [];// 新获得的头像
    wxHeadAsset: cc.Texture2D;//缓存玩家的微信头像
    initPiFuList;

    KEY_OPENED_ARENA = "KEY_OPENED_ARENA"

    constructor() {
        super('user');
    }

    onRegister(): void {
        GameDataCenter.gameServer.ListenMsg("eps/server/EpsChange_s", this.onEpsChange, this);
    }

    onEpsChange(msg: MsgEpsChange_s) {
        for (let key in msg) {
            if (Object.prototype.hasOwnProperty.call(msg, key)) {
                this.epsInfo[key] = msg[key];
            }
        }
        EventMng.emit(UserEvent.UP_USER_EPS);
    }

    /*************** 协议推送消息结束 ***********/

    ////////// 业务阶段所需参数 //////////start

    setUserInfo(userInfo: ResUserLogin) {
        this.userInfo = userInfo;
    }

    setEpsInfo(epsInfo: ResEpsInfo) {
        this.epsInfo = epsInfo;
    }

    get uuid(): string {

        return this.userInfo?.uuid || "";
    }
    get token(): string {
        if (gameMethod.isEmpty(GameDataCenter.sevBack)) { return "" }
        if (gameMethod.isEmpty(GameDataCenter.sevBack.userInfo)) { return "" }
        let userData = GameDataCenter.sevBack.userInfo.a
        if (userData != null && userData.token != null) {
            return userData.token
        }
        return ""
    }
    get serverName(): string {
        if (gameMethod.isEmpty(GameDataCenter.sevBack)) { return null }
        if (gameMethod.isEmpty(GameDataCenter.sevBack.userInfo)) { return null }
        if (gameMethod.isEmpty(GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo.a.sid])) {
            return null
        }
        return GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo.a.sid].name
    }

    get nickName(): string {
        if (gameMethod.isEmpty(GameDataCenter.sevBack?.userInfo?.a?.name)) { return "" }

        return GameDataCenter.sevBack.userInfo.a.name;
    }

    get userHead(): string {
        if (gameMethod.isEmpty(GameDataCenter.sevBack?.userInfo?.a?.head)) { return "" }

        return GameDataCenter.sevBack.userInfo.a.head;
    }


    getServerNameBySid(sid: string) {
        if (gameMethod.isEmpty(GameDataCenter.sevBack)) {
            return null
        }
        if (gameMethod.isEmpty(GameDataCenter.sevBack.qufuList[sid])) {
            return null
        }
        return GameDataCenter.sevBack.qufuList[sid].name
    }

    // 返回 今天是服务器开启第几天
    getSevOpenDay() {
        return GameDataCenter.sevBack.other?.kfdays || 0
    }

    sendPlayerReconnect(cb?: Function) {
        // if (GameDataCenter.sevBack?.playerInfo != null) {
        //     GameController.network.stopRequest = false
        //     let param: PlayerReconnectPram = {
        //         uid: GameDataCenter.sevBack.playerInfo.uid,
        //         sid: GameDataCenter.sevBack.playerInfo.sid,
        //         token: GameDataCenter.sevBack.playerInfo.token
        //     }
        //     this.send(PlayerReconnect.url, param, (result: SevBack) => {
        //         if (result.type == 1) {
        //             if (cb) {
        //                 cb()
        //             }
        //         }
        //     })
        // }
    }

    // 切换区服
    sendChangeServer(sid: string) {
        // let param: UserHuanFuPram = { sid: sid }
        // this.send(UserHuanFu.url, param, (result: SevBack) => {
        //     if (result.type == 1) {
        //         if (cc.sys.platform == cc.sys.ANDROID || cc.sys.platform == cc.sys.IPHONE || cc.sys.platform == cc.sys.IPAD) {
        //             NativeManager.CallToNative("logoutSdk", [], "")
        //             GameDataCenter.plat.instance.restart()
        //         } else {
        //             GameDataCenter.plat.instance.restart()
        //         }

        //     }
        // })
    }
    // 加载头像
    loadUserHead(node: cc.Node, user: { wxhead: string, head: string }) {

        if (node.getComponent(cc.Sprite) == null) {
            node.addComponent(cc.Sprite)
        }

        let wxUrl = user.wxhead;
        // 判断是否有远程图片
        if (!gameMethod.isEmpty(wxUrl) && wxUrl.indexOf('http') >= 0) {
            // 判断是否选择着的是默认头像 默认头像就变成远程图片
            if (gameMethod.isEmpty(user.head) || user.head == 'zj_1') {
                // 加载远程图片
                cc.assetManager.loadRemote(wxUrl, { ext: ".png" }, (err, asset: cc.Texture2D) => {
                    if (err) {
                        console.log("err,", err)
                        return
                    }
                    node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(asset);
                })
            } else {
                // 加载选择的头像
                Load.loadTexture(node, `setting/resHeadIcon/${user.head}`);
            }
        } else {
            // 没有远程图片 判断是否默认头像
            if (gameMethod.isEmpty(user.head) || user.head == 'zj_1') {
                Load.loadTexture(node, "setting/resHeadIcon/zj_1");
            } else {
                // 加载选择的头像
                if (user.head.indexOf('http') >= 0) {
                    // 加载远程图片
                    cc.assetManager.loadRemote(user.head, { ext: ".png" }, (err, asset: cc.Texture2D) => {
                        if (err) {
                            console.log("err,", err)
                            return
                        }
                        node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(asset);
                    })
                } else {
                    Load.loadTexture(node, `setting/resHeadIcon/${user.head}`);
                }
            }
        }
    }

    saveWxHeadAssets(texture: cc.Texture2D) {
        if (gameMethod.isEmpty(this.wxHeadAsset)) {
            this.wxHeadAsset = texture;
        }
    }

    GetMyHeadUrl() {
        return this.GetHeadUrl(GameDataCenter.sevBack.userInfo?.a?.sex, GameDataCenter.sevBack.userInfo?.a?.head);
    }

    GetHeadUrl(sex: UserSex, head: string, wxUrl?: string): string {
        // 判断是否有远程图片
        if (!gameMethod.isEmpty(wxUrl) && wxUrl.indexOf('http') >= 0) {
            // 判断是否选择着的是默认头像 默认头像就变成远程图片
            if (gameMethod.isEmpty(head) || head == '1') {
                // 加载远程图片
                return wxUrl;
            } else {
                // 加载选择的头像
                return `ui://FollowerIcon/zj_${head}`;
            }
        } else {
            //没有图片 使用默认图片
        }
    }

    // 获取性别类型
    public GetSexType() {
        // if (!gameMethod.isEmpty(GameDataCenter?.sevBack?.userInfo?.a?.sex)) {
        this.roleSexType = GameDataCenter?.sevBack?.userInfo?.a?.sex;
        // }
        return this.roleSexType;
    }

    async sendChangeName(name: string) {
        let ret = await GameDataCenter.gameServer.ReqApi('user/UserSetName', { name: name });
        if (ret?.res) {
            console.log(ret);
            this.userInfo.name = ret.res.name;
            UIHelp.ShowTips("修改名称成功");
            EventMng.emit(UserEvent.UP_USER);
        }
    }

    async sendEpsInfo() {
        let ret = await GameDataCenter.gameServer.ReqApi("eps/EpsInfo", {});
        if (ret?.res) {
            this.setEpsInfo(ret.res);
            EventMng.emit(UserEvent.UP_USER_EPS);
        }
    }
}