import { _decorator, Component, instantiate, Label, Node, sp, Sprite, SpriteFrame, UITransform } from 'cc';
import { I18nManager } from '../../../Loading/scripts/manager/I18nManager';
import { Icon } from './Icon';
import { ICON_RATE } from './Define';
import { AudioManager } from 'db://assets/Loading/scripts/manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('IconMsg')
export class IconMsg extends Component {

    @property(Node)
    leftNode: Node = null;

    @property(Node)
    rightNode: Node = null;

    iconComponent: Icon = null;

    ui_payout: Node = null;
    ui_payout_wild: Node = null;
    ui_payout_scatter: Node = null;

    multi: Node = null;
    sprite_wild: Node = null;
    sprite_scatter: Node = null;

    right_ui_payout: Node = null;
    right_ui_payout_wild: Node = null;
    right_ui_payout_scatter: Node = null;

    right_multi: Node = null;
    right_sprite_wild: Node = null;
    right_sprite_scatter: Node = null;

    // 高度映射表 - 减少重复计算
    HEIGHT_MAP = {
        1: 190,
        2: 190 + 145,
        3: 190 + 145 * 2,
        4: 190 + 145 * 3
    };


    show(isLeft: boolean, iconComponent: Icon) {
        AudioManager.instance.playSFX('Appear_Window_Sound');
        this.iconComponent = iconComponent;

        this.leftNode.active = false;
        this.rightNode.active = false;

        this.ui_payout = this.leftNode.getChildByName('ui_payout');
        this.ui_payout_wild = this.leftNode.getChildByName('ui_payout_wild');
        this.ui_payout_scatter = this.leftNode.getChildByName('ui_payout_scatter');

        this.multi = this.leftNode.getChildByName('multi');
        this.sprite_wild = this.leftNode.getChildByName('sprite_wild');
        this.sprite_scatter = this.leftNode.getChildByName('sprite_scatter');

        this.ui_payout.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];
        this.ui_payout_wild.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];
        this.ui_payout_scatter.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];


        this.right_ui_payout = this.rightNode.getChildByName('ui_payout');
        this.right_ui_payout_wild = this.rightNode.getChildByName('ui_payout_wild');
        this.right_ui_payout_scatter = this.rightNode.getChildByName('ui_payout_scatter');

        this.right_multi = this.rightNode.getChildByName('multi');
        this.right_sprite_wild = this.rightNode.getChildByName('sprite_wild');
        this.right_sprite_scatter = this.rightNode.getChildByName('sprite_scatter');

        this.right_ui_payout.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];
        this.right_ui_payout_wild.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];
        this.right_ui_payout_scatter.getComponent(UITransform).height = this.HEIGHT_MAP[iconComponent.lHeight];


        this.ui_payout.active = false;
        this.ui_payout_wild.active = false;
        this.ui_payout_scatter.active = false;

        this.right_ui_payout.active = false;
        this.right_ui_payout_wild.active = false;
        this.right_ui_payout_scatter.active = false;

        this.multi.active = false;
        this.sprite_wild.active = false;
        this.sprite_scatter.active = false;

        this.right_multi.active = false;
        this.right_sprite_wild.active = false;
        this.right_sprite_scatter.active = false;

        if (iconComponent.index == 0) {
            this.ui_payout_wild.active = true;
            this.sprite_wild.active = true;
            this.right_ui_payout_wild.active = true;
            this.right_sprite_wild.active = true;
        } else if (iconComponent.index == 1) {
            this.ui_payout_scatter.active = true;
            this.sprite_scatter.active = true;
            this.right_ui_payout_scatter.active = true;
            this.right_sprite_scatter.active = true;
        } else {
            this.ui_payout.active = true;
            this.multi.active = true;
            this.right_ui_payout.active = true;
            this.right_multi.active = true;
        }


        if (isLeft) {
            this.leftNode.active = true;
            this.setLeftMsg();
        } else {
            this.rightNode.active = true;
            this.setRightMsg();
        }
    }

    setLeftMsg() {
        let iconNode = instantiate(this.iconComponent.node);
        iconNode.setParent(this.leftNode.getChildByName('node'));
        iconNode.setPosition(0, 0, 0);
        this.scheduleOnce(() => {
            iconNode.getComponent(Icon).index = this.iconComponent.index;
            iconNode.getComponent(Icon).hideNormalSprite();
            iconNode.getComponent(Icon).playSpawnAni();
        })

        this.multi.children.forEach((child, index) => {
            let multi = ICON_RATE[this.iconComponent.index][index];
            child.getChildByName('multi').getComponent(Label).string = multi.toString();
        })
    }

    setRightMsg() {
        let iconNode = instantiate(this.iconComponent.node);
        iconNode.setParent(this.rightNode.getChildByName('node'));
        iconNode.setPosition(0, 0, 0);
        this.scheduleOnce(() => {
            iconNode.getComponent(Icon).index = this.iconComponent.index;
            iconNode.getComponent(Icon).hideNormalSprite();
            iconNode.getComponent(Icon).playSpawnAni();
        })


        this.right_multi.children.forEach((child, index) => {
            let multi = ICON_RATE[this.iconComponent.index][index];
            child.getChildByName('multi').getComponent(Label).string = multi.toString();
        })
    }

    hide() {
        this.leftNode.getChildByName('node').children.forEach(child => {
            child.destroy();
        })
        this.rightNode.getChildByName('node').children.forEach(child => {
            child.destroy();
        })
    }
}


