// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import LocalStorage from "../Data/LocalStorage";
import GameManager from "../GamePlay/GameManager";
import Message from "./Message";
import PopupUI from "./Popup/PopupUI";
import ScreenUI from "./Screen/ScreenUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIManager extends cc.Component {
    @property([ScreenUI])
    screens: ScreenUI[] = [];
    @property([PopupUI])
    popups: PopupUI[] = [];

    @property(cc.Node)
    transparentPopup: cc.Node = null;
    
    @property(Message)
    message: Message = null;

    @property(cc.Sprite)
    spriteBackground: cc.Sprite = null;

    @property([cc.SpriteFrame])
    spriteFrameBackground: cc.SpriteFrame[] = [];

    gameManager: GameManager = null;

    public static Instance: UIManager = null;
    
    protected onLoad(): void {
        UIManager.Instance = this;
    }

    initialize(manager: GameManager) {

        this.gameManager = manager;

        for (let i = 0; i < this.screens.length; i++) {
            this.screens[i].initialize(this);
        }

        for (let i = 0; i < this.popups.length; i++) {
            this.popups[i].initialize(this);
        }
        this.setBackground(LocalStorage.getBackground());
    }

    deactiveAllScreen() {
        for (let i = 0; i < this.screens.length; i++) {
            this.screens[i].deactive();
        }
    }

    activeScreen(screenName, hideAll = true) {
        let screen = null;
        for (let i = 0; i < this.screens.length; i++) {
            if (this.screens[i].screenName == screenName) {
                this.screens[i].node.active = true;
                this.screens[i].active();
                screen = this.screens[i].getComponent(cc.Component);
            } else {
                if (hideAll) {
                    this.screens[i].deactive();
                }
            }
        }
        return screen;
    }

    getScreen(screenName) {
        for (let i = 0; i < this.screens.length; i++) {
            if (this.screens[i].screenName == screenName) {
                return this.screens[i].getComponent(cc.Component);
            }
        }
        return null;
    }

    showPopup(popupName) {
        let popup = null;
        for (let i = 0; i < this.popups.length; i++) {
            if (this.popups[i].popupName == popupName) {
                this.popups[i].show();
                popup = this.popups[i].getComponent(cc.Component);
            }
        }
        return popup;
    }

    activeTransparent(){
        this.transparentPopup.active = true;
    }

    deActiveTransparent(){
        this.transparentPopup.active = false;
    }

    getPopup(popupName) {
        let popup = null;
        for (let i = 0; i < this.popups.length; i++) {
            if (this.popups[i].popupName == popupName) {
                popup = this.popups[i].getComponent(cc.Component);
            }
        }
        return popup;
    }

    showMessage(message: string){
        this.message.showMessage(message);
    }

    setBackground(idBackground: number){
        this.spriteBackground.spriteFrame = this.spriteFrameBackground[idBackground];
    }
}
