import { _decorator, AnimationClip, Label, Node, Animation, Button } from 'cc';
import { UIPanel } from '../base/UIPanel';
import { EventOperations, EventsType } from '../../Managers/global/interface/IPrimary';
import { Cards_Constants } from '../../constants/Cards_Constants';
import { GameManager } from '../../Managers/global/state/GameManager';
import { ManagerType } from '../../Managers/global/data-flow/TopLevelManager';
const { ccclass, property } = _decorator;

@ccclass('HUDPanel')
export class HUDPanel extends UIPanel
{
    @property({type: Animation})
    private animation: Animation;

    @property({type: AnimationClip})
    private hudDropDownClip: AnimationClip;

    @property({type: AnimationClip})
    private hudDropUpClip: AnimationClip;

    @property({type: Label})
    public m_MatchesLabel: Label;

    @property({type: Label})
    public m_TurnsLabel: Label;

    @property({type: Node, group: "Hint Bar"})
    public m_HintBarNode: Node;

    @property({type: Button, group: "Hint Bar"})
    public m_HintBar1: Button;
    
    @property({type: Animation, group: "Hint Bar Animation"})
    public m_HintBarAnim: Animation;
    
    @property({type: AnimationClip, group: "Hint Bar Animation"})
    public m_HintBarClip: AnimationClip;
    
    @property({type: Node})
    public m_SettingsButton: Node;

    @property({type: Label})
    private levelLabelDebugText: Label;

    private onHintBarClicked: boolean;
    private maxPairs: number;
    private maxTurnsCount: number;

    protected onEnable(): void 
    {
        super.onEnable();


        this.fn_SetSettingsBtnState(true);
        this.onHintBarClicked = false;

        this.m_HintBarNode.on(Button.EventType.CLICK, () => this.fn_OnHintBarClick());    
        
    }
    
    public fn_OnHintBarClick()
    {
        this.onHintBarClicked = true;
        this.fn_HideHintBar();
    }
    
    protected onDisable(): void 
    {
        this.m_HintBarNode.off(Button.EventType.CLICK, () => this.fn_OnHintBarClick());    
    }

    public fn_PlayHudDropUpAnim()
    {
        // this.animation.play(this.hudDropUpClip.name);
    }

    public fn_InitInfo(maxPairs: number, maxTries: number) // also initialize turnsCount
    {
        this.maxPairs = maxPairs;
        this.maxTurnsCount = maxTries;
        this.levelLabelDebugText.string = this.uiManager.MainManager.fn_GetMainManager<GameManager>(ManagerType.GAME).m_LevelManager.fn_GetCurrentLevelNum().toString();
    }

    public fn_UpdateMatchesLabel(matchesCount: number)
    {
        this.uiManager.MainManager.fn_GetMainManager<GameManager>(ManagerType.GAME).m_CardManager.GetTotalPairs;
        this.m_MatchesLabel.string = "" + matchesCount + "/" + this.maxPairs;
    }

    // private fn_GetStringBasedOnDigits(digitNum: number)
    // {
    //     let len = digitNum.toString().length;
    //     if (len == 1)
    //         return "00";
    //     else if (len == 2)
    //         return "0";

    //     return "";
    // }

    public fn_UpdateTurnsLabel(turnsCount: number)
    {
        this.m_TurnsLabel.string = "" + turnsCount + "/" + this.maxTurnsCount;
    }

    public fn_ShowHintBar(state: boolean)
    {
        this.m_HintBarNode.active = state;
        if (this.m_HintBarNode.active)
        {
            this.fn_PassEventData(EventOperations.ATTACH, EventsType.HINT_BAR);

            this.m_HintBarAnim.play(this.m_HintBarClip.name);
        }
    }

    public fn_HideHintBar()
    {
        if (this.m_HintBarNode.active)
        {
            if (this.onHintBarClicked)
            {
                this.onHintBarClicked = false;
                this.fn_PassEventData(EventOperations.FIRE, EventsType.HINT_BAR);
            }
            
            this.fn_PassEventData(EventOperations.DETACH, EventsType.HINT_BAR);

            this.m_HintBarNode.active = false;
        }
    }

    public fn_SetSettingsBtnState(state: boolean)
    {
        this.m_SettingsButton.active = state;
    }
}

