import { _decorator, Button, Component, Label, Node, Sprite, Color, SpriteFrame } from 'cc';
import { LevelPanel } from '../../UI/global/LevelPanel';
import { LevelData } from '../../Managers/data/DataModel';
import { LevelLabel } from './LevelLabel';
const { ccclass, property } = _decorator;

export const ALPHA_VAL = 255;

@ccclass('Level')
export class Level extends Component 
{
    @property({type: Button})
    private m_LevelButton: Button;

    @property({type: SpriteFrame})
    private m_UnlockSprite: SpriteFrame;

    @property({type: SpriteFrame})
    private m_LockSprite: SpriteFrame;

    @property({type: Sprite})
    private m_LevelIcon: Sprite;

    @property({type: Sprite})
    private m_ShipSprite: Sprite;

    @property({type: Sprite})
    private m_SailSprite: Sprite;

    @property({type: Color})
    private unlockedSailSpriteColor: Color;

    @property({type: Color})
    private lockedSailSpriteColor: Color;

    @property({type: Node})
    private m_Nest: Node;

    private levelNum: number;
    private row: number;
    private col: number;
    private unlockState: boolean;

    private levelPanel: LevelPanel; 
    private levelData: LevelData;
    
    private levelLabelComp: LevelLabel;

    public fn_GetLevelNum() : number
    {
        return this.levelNum;
    }

    public fn_GetRow() : number
    {
        return this.row;
    }

    public fn_GetCol() : number
    {
        return this.col;
    }

    public fn_GetUnlockState() : boolean
    {
        return this.unlockState;
    }

    public fn_SetLevelData(data: LevelData)
    {
        this.levelData = data;
    }

    public fn_Init(levelPanel: LevelPanel, levelLabel: LevelLabel)
    {
        this.levelLabelComp = levelLabel;
        if (this.levelData == null) 
        {
            this.fn_ToggleLevelObj(false); 
            return;
        }
        
        this.levelNum = this.levelData.level;
        this.levelLabelComp.fn_Init(this.levelData.level.toString());
        this.unlockState = this.levelData.unlocked;
        
        this.levelPanel = levelPanel;
        this.row = this.levelData.row;
        this.col = this.levelData.col;
        
        this.fn_SetLevelLockState(this.levelData.unlocked);
        this.fn_ToggleLevelObj(true); 
    }

    public fn_SetLevelLockState(unlocked: boolean)
    {
        this.levelData.unlocked = unlocked;
        this.unlockState = unlocked;

        this.m_LevelButton.interactable = unlocked;
        this.m_SailSprite.spriteFrame = unlocked ? this.m_UnlockSprite : this.m_LockSprite;
        this.m_SailSprite.color = unlocked ? this.unlockedSailSpriteColor: this.lockedSailSpriteColor;
        
        this.fn_ToggleSelection(unlocked && this.levelPanel.lastSelectedLevel == this.levelNum);

        // if (this.levelNum == this.levelPanel.lastUnlockedLevel)
        // {
        //     this.fn_ToggleSelection(true);
        //     this.levelPanel.fn_SetPrevAndCurrentLevelElements(this, true);
        // }
        // else 
        //     this.fn_ToggleSelection(false);
            
        this.levelLabelComp.fn_SetLevelLockState(unlocked);
    }

    public fn_OnClick()
    {
        this.fn_ToggleSelection(true);
        this.levelPanel.fn_SetLastUnlockedLevel(this.levelNum);
        this.levelPanel.fn_SetPrevAndCurrentLevelElements(this);
    }

    public fn_ToggleSelection(state: boolean)
    {
        this.m_ShipSprite.enabled = state;
        this.m_Nest.active = state;
    }

    public fn_ToggleLevelObj(state: boolean)
    {
        this.node.active = state;
        this.levelLabelComp.node.active = state;
    }
}

