using System;
using UnityEngine;

namespace NBF
{
    public class FishWeightToLength : MonoBehaviour
    {
        [Serializable]
        public class FishWeightData
        {
            [SerializeField] private string FishName = "Nazwa rybki";

            public FishSpecies species;

            [Tooltip("X - waga ryby, Y - centymetry")]
            public Vector2[] weightLenghtValues;

            public AnimationCurve weightLengthCurve;

            public void SetupCurvesWeight()
            {
                weightLengthCurve.keys = null;
                for (int i = 0; i < weightLenghtValues.Length; i++)
                {
                    weightLengthCurve.AddKey(weightLenghtValues[i].x, weightLenghtValues[i].y);
                }
            }
        }

        private static FishWeightToLength instance;

        [SerializeField] [Tooltip("Uzywac tylko w edytorze")]
        private bool isEditMode = true;

        public FishWeightData[] fishWeightData;

        [Tooltip("Testowanie poprawnosci konwersji waga/centymetry")] [SerializeField]
        private FishSpecies TestSpecies;

        [SerializeField] private float TestWeight;

        [SerializeField] private float TestWynikCentymetry;

        public static FishWeightToLength Instance => instance;

        private void Awake()
        {
            DontDestroyOnLoad(this);
            if (instance == null)
            {
                instance = this;
            }
            else if (instance != this)
            {
                Destroy(gameObject);
            }
        }

        private void Start()
        {
        }

        private void Update()
        {
            if (!isEditMode)
            {
                return;
            }

            for (int i = 0; i < fishWeightData.Length; i++)
            {
                fishWeightData[i].SetupCurvesWeight();
                if (TestSpecies == fishWeightData[i].species)
                {
                    TestWynikCentymetry = fishWeightData[i].weightLengthCurve.Evaluate(TestWeight);
                }
            }
        }

        public float ConvertWeightFishToLength(FishSpecies species, float weight)
        {
            for (int i = 0; i < fishWeightData.Length; i++)
            {
                if (fishWeightData[i].species == species)
                {
                    fishWeightData[i].SetupCurvesWeight();
                    return fishWeightData[i].weightLengthCurve.Evaluate(weight);
                }
            }

            return 0f;
        }
    }
}