import CollisionDetection from "./CollisionDetection";
import JiaZai from "./JiaZai";
import MapConroler from "./Map";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Avatar extends cc.Component {

    // @property({
    //     tooltip: '碰撞形状，None就是无敌，不参与碰撞',
    //     type: cc.Enum(BlockType),
    //     // default: BlockType.Nomal,
    //     displayName: '碰撞形状'
    // })
    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Node)
    content2: cc.Node = null;

    @property(cc.SpriteAtlas)
    ui: cc.SpriteAtlas = null;

    avatar: any;  //正在使用中的头像

    avatarNow: any// 选中的头像
    avatarKuang: any;





    onLoad() {
    }
    start() {
    }
    init() {
        this.avatar = cc.fx.GameConfig.GM_INFO.useravatarIcon;
        this.avatarKuang = cc.fx.GameConfig.GM_INFO.useravaterkuang;
        this.changeBtnState("avatar");
        this.changeAvatar(null, this.avatar);
        this.getSelfAvatar();
    }

    getSelfAvatar() {
        this.content2.getChildByName(this.avatarKuang).getChildByName("select").active = true;
        if (this.avatar.length > 10) {
            this.setSelfAvatar(true);
        }
        else {
            this.node.getChildByName("self").getChildByName("icon").getComponent(cc.Sprite).spriteFrame =
                this.ui.getSpriteFrame(this.avatar);
        }
        this.node.getChildByName("self").getChildByName("kuang").getComponent(cc.Sprite).spriteFrame =
            this.ui.getSpriteFrame(this.avatarKuang);
        cc.fx.GameTool.getUserAvatar((data) => {
            if (data == true) {
                this.avatar = cc.fx.GameConfig.GM_INFO.useravatar;
                this.changeAvatar(null, this.avatar);
                this.clickBtn();
            }
            this.setSelfAvatar(data);
        })
    }

    closeAvatar() {
        const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
        const jiazaiComp = jiazaiNode.getComponent(JiaZai);
        if (jiazaiComp) {
            console.log("获取到JiaZai组件", jiazaiComp);
            jiazaiComp.closeAvatar();

        } else {
            console.log("无法获取JiaZai组件");
        }
    }

    // 切换状态 头像和头像框
    changeState(event, state) {
        if (state == 'tou') {
            if (this.node.getChildByName("tou").opacity == 255) {
                return;
            }
            else {
                this.node.getChildByName("tou").opacity = 255;
                this.node.getChildByName("kuang").opacity = 0;
                this.node.getChildByName("avatar").active = true;
                this.node.getChildByName("avatarKuang").active = false;
                this.changeBtnState("avatar");
            }
        }
        else if (state == 'kuang') {
            if (this.node.getChildByName("kuang").opacity == 255) {
                return;
            }
            else {
                this.node.getChildByName("tou").opacity = 0;
                this.node.getChildByName("kuang").opacity = 255;
                this.node.getChildByName("avatar").active = false;
                this.node.getChildByName("avatarKuang").active = true;
                this.changeBtnState("kuang");
            }
        }
    }

    // 判断按钮状态，切换按钮状态
    changeBtnState(state) {
        if (state == 'avatar') {
            if (this.avatar == cc.fx.GameConfig.GM_INFO.useravatarIcon) {
                this.node.getChildByName("btn").active = false;
                this.node.getChildByName("btnUse").active = true;
            }
            else {
                this.node.getChildByName("btn").active = true;
                this.node.getChildByName("btnUse").active = false;
            }
        }
        else if (state == 'kuang') {
            if (this.avatarKuang == cc.fx.GameConfig.GM_INFO.useravaterkuang) {
                this.node.getChildByName("btn").active = false;
                this.node.getChildByName("btnUse").active = true;
            }
            else {
                this.node.getChildByName("btn").active = true;
                this.node.getChildByName("btnUse").active = false;
            }
        }
    }

    //改变头像
    changeAvatar(event, avatar) {
        this.closeSelectAvatar();
        this.avatar = avatar;
        switch (avatar) {
            case "icon":
                this.avatar = cc.fx.GameConfig.GM_INFO.useravatar;
                this.content.children[0].getChildByName("select").active = true;
                break;
            case "icon_0":
                this.content.children[1].getChildByName("select").active = true;
                break;
            case "icon_1":
                this.content.children[2].getChildByName("select").active = true;
                break;
            case "icon_2":
                this.content.children[3].getChildByName("select").active = true;
                break;
            case "icon_3":
                this.content.children[4].getChildByName("select").active = true;
                break
            default:
                if (this.avatar.length > 10) {
                    this.content.children[0].getChildByName("select").active = true;
                }

                break;
        }
        this.changeBtnState("avatar");
    }

    setSelfAvatar(data) {
        let url = cc.fx.GameConfig.GM_INFO.useravatar;
        if (url == "") {
            return;
        }
        var self = this;
        cc.assetManager.loadRemote(cc.fx.GameConfig.GM_INFO.useravatar, { ext: '.png' }, (err, texture: cc.Texture2D) => {
            if (texture) {
                if (data == true || this.avatar.length > 10) {
                    var sprite = self.node.getChildByName("self").getChildByName("icon").getComponent(cc.Sprite);
                    sprite.spriteFrame = new cc.SpriteFrame(texture);
                }
                var sprite2 = self.content.children[0].getChildByName("icon").getComponent(cc.Sprite);
                sprite2.spriteFrame = new cc.SpriteFrame(texture);
            }
        })
    }

    //使用 按钮
    clickBtn() {
        if (this.node.getChildByName("avatar").active == true) {
            cc.fx.GameConfig.GM_INFO.useravatarIcon = this.avatar;
            if (cc.fx.GameConfig.GM_INFO.useravatarIcon.length > 10) {
                cc.fx.GameConfig.GM_INFO.useravatarIcon = cc.fx.GameConfig.GM_INFO.useravatar;
                this.setSelfAvatar(true);
            }
            else {
                this.node.getChildByName("self").getChildByName("icon").getComponent(cc.Sprite).spriteFrame =
                    this.ui.getSpriteFrame(this.avatar);
            }
        }
        else {
            cc.fx.GameConfig.GM_INFO.useravaterkuang = this.avatarKuang;
            this.node.getChildByName("self").getChildByName("kuang").getComponent(cc.Sprite).spriteFrame =
                this.ui.getSpriteFrame(this.avatarKuang);
        }
        this.node.getChildByName("btn").active = false;
        this.node.getChildByName("btnUse").active = true;
    }

    //改变头像框
    changeKuang(event, kuang) {
        this.closeSelectKuang();
        this.avatarKuang = kuang;
        this.content2.getChildByName(kuang).getChildByName("select").active = true;
        this.changeBtnState("kuang");
    }

    closeSelectAvatar() {
        for (let i = 0; i < this.content.children.length; i++) {
            this.content.children[i].getChildByName("select").active = false;
        }
    }

    closeSelectKuang() {
        for (let i = 0; i < this.content2.children.length; i++) {
            this.content2.children[i].getChildByName("select").active = false;
        }
    }




}


