import { gameMethod } from "../../../common/gameMethod";
import GameDataCenter from "../../../data/GameDataCenter";
import { HeadBgType } from "../../../data/const/TypeConst";
import { UserInfo } from "../../../data/model/User/UserModelData";
import ResSprite from "../../../frameWork/compment/ResSprite";
import FguiLoadMgr from "../../../frameWork/fgui/FguiLoadMgr";
import ClickAuEffect from "../../../utils/ClickAuEffect";


export default class UserHead extends fgui.GComponent {
    uuid: string;
    lookDetails: boolean;
    touchStart: number;

    setData(url: string) {
        this.getChild("ZheZhao").visible = true;
   
        if (!gameMethod.isEmpty(url) && url.indexOf('http') >= 0) {
            //加载远程头像
            if (this.uuid && this.uuid == GameDataCenter.user.uuid && !gameMethod.isEmpty(GameDataCenter.user.wxHeadAsset)) {
                let spriteCom = this.getChild("ZheZhao").asCom.getChild("HeadImg").node.getComponent(cc.Sprite);
                if (gameMethod.isEmpty(spriteCom)) {
                    spriteCom = this.getChild("ZheZhao").asCom.getChild("HeadImg").node.addComponent(cc.Sprite);
                }
                this.getChild("ZheZhao").asCom.getChild("HeadImg").asLoader.url = "";
                spriteCom.spriteFrame = new cc.SpriteFrame(GameDataCenter.user.wxHeadAsset);
                return;
            }
            cc.assetManager.loadRemote(url, { ext: ".png" }, (err, asset: cc.Texture2D) => {
                if (err) {
                    console.log("err,", err)
                    return
                }
                this.getChild("ZheZhao").asCom.getChild("HeadImg").asLoader.url = "";
                let spriteCom = this.getChild("ZheZhao").asCom.getChild("HeadImg").node.getComponent(cc.Sprite);
                if (gameMethod.isEmpty(spriteCom)) {
                    spriteCom = this.getChild("ZheZhao").asCom.getChild("HeadImg").node.addComponent(cc.Sprite);
                }
                spriteCom.spriteFrame = new cc.SpriteFrame(asset);
                if (this.uuid && this.uuid == GameDataCenter.user.uuid) {
                    GameDataCenter.user.saveWxHeadAssets(asset);
                }
            })
        } else if (!gameMethod.isEmpty(url) && url.indexOf('ui://') >= 0) {
            //动态加载fgui资源
            let bundleName: string = url.split("/")[2];
            let spriteCom = this.getChild("ZheZhao").asCom.getChild("HeadImg").node.getComponent(cc.Sprite);
            if (!gameMethod.isEmpty(spriteCom)) {
               spriteCom.spriteFrame = null;
            }
            FguiLoadMgr.loadSpriteFormPackage(this.getChild("ZheZhao").asCom.getChild("HeadImg").asLoader, bundleName, bundleName, url);
           
        } else {
            if (gameMethod.isEmpty(url)) {
                return;
            }
            //加载bundle资源
            let bundleName: string = url?.split("|")[0];
            let urlStr: string = url?.split("|")[1];
            this.getChild("ZheZhao").asCom.getChild("HeadImg").asLoader.url = "";
            GameDataCenter.loading.loadSpriteFormBundle(this.getChild("ZheZhao").asCom.getChild("HeadImg")?.node, bundleName, urlStr);
        }
    }

    protected onEnable(): void {
        this.on(fgui.Event.TOUCH_BEGIN, this.onTouchBegin, this);//防止聊天长按也触发按钮效果，所以改用监听触摸短按
        this.on(fgui.Event.TOUCH_END, this.onTouchEnd, this);
    }

    protected onDisable(): void {
        this.off(fgui.Event.TOUCH_BEGIN, this.onTouchBegin, this);
        this.off(fgui.Event.TOUCH_END, this.onTouchEnd, this);
    }

    onTouchBegin() {
        this.touchStart = new Date().getTime();
    }

    onTouchEnd() {
        let touchTime = new Date().getTime() - this.touchStart;
        if (touchTime < 500) {
            this.onClickHead();
        }
    }

    setHead(user: { userInfo: UserInfo, headId?: string, colorBg?: HeadBgType, isFlip?: boolean, lookDetails?: boolean }) {
        this.uuid = user.userInfo?.uuid;
        if (user?.headId) {
            this.setData(GameDataCenter.user.GetHeadUrl(user.userInfo?.sex, user.headId));
        }
        else {
            this.setData(GameDataCenter.user.GetHeadUrl(user.userInfo?.sex, user.userInfo?.head, user.userInfo?.wxhead));
        }
        this.lookDetails = user.lookDetails;
        if (user.colorBg) {
            this.getController("BgColor").selectedIndex = user.colorBg;
        }
        else {
            this.getController("BgColor").selectedIndex = HeadBgType.red;
        }

        this.getChild("ZheZhao").asCom.scaleX = user.isFlip && gameMethod.isEmpty(user?.userInfo?.wxhead) ? -1 : 1;
    }
    @ClickAuEffect()
    onClickHead() {
        if (gameMethod.isEmpty(this.uuid)) return;
        if (this.lookDetails) {
            // GameDataCenter.user.sendUserLookFuuidAll(this.uuid);
        }
    }

    SetEmpty() {
        this.getChild("ZheZhao").visible = false;
    }
}
