﻿using UnityEngine;
using UnityEngine.UI;

public abstract class FwProfile : MonoBehaviour
{
    private FwProfileDataCalculator _calculator;
    private FwProfileData _data;

    public int Damage;
    public int Health;

    public int PlayerLevel
    {
        get { return _data.Level; }
        set
        {
            if (value <= 0) return;
            _data.Level = value;
            CalculateAndFillData();
        }
    }

    private void Setup()
    {
        if (!IsInitialized())
            InitAll();
        SetPropertyPlayerLevel();
        CalculateAndFillData();
        SetupKillable();
        SetupObject();
    }

    private void CalculateAndFillData()
    {
        CalculateData();
        FillData();
    }

    private void CalculateData()
    {
        _data.Health = _calculator.CalculatedHealthValue(_data.Level);
        _data.Damage = _calculator.CalculatedDamageValue(_data.Level);
    }

    private void FillData()
    {
        Health = _data.Health;
        Damage = _data.Damage;
    }

    private void InitAll()
    {
        _data = new FwProfileData();
        _calculator = new FwProfileDataCalculator();
    }

    private bool IsInitialized()
    {
        return !(null == _data || null == _calculator);
    }

    #region Abstract methods
    protected abstract void SetupKillable();

    protected abstract void SetupObject();

    protected abstract void SetPropertyPlayerLevel();
    #endregion

    #region Mono methods
    private void OnVisible()
    {
        Setup();
    }

    private void OnEnable()
    {
        Setup();
    }

    private void Start()
    {
        Setup();
    }
    #endregion
}