using System;
using System.Collections;
using System.Collections.Generic;
using NBF;
using Unity.VisualScripting;
using UnityEngine;

public class FishFeedingZone : MonoBehaviour
{
    [Serializable]
    public class FishPopulation
    {
        public FishSpecies FishSpecies;

        public int AmountPopulationZone = 1;

        public Vector2 weightRange;
    }

    public bool fishTakeTesting;

    public int Inedex;

    public int InedexOfGroup;

    public float rangeZone = 5f;

    public FishPopulation[] fishPopulation;

    public List<FFish> fishesOnZone;

    public FFishSystem fFishSystem;
    public Vector3 selfV3;

    private void Start()
    {
        fFishSystem = FindObjectOfType<FFishSystem>();
        fishesOnZone = new List<FFish>();
    }

    private void Update()
    {
        if (!fFishSystem.isNewSpawnerSystem)
        {
            checkAndUpdateFishPopulation();
        }
    }

    public IEnumerator GenerateOneFish(Vector3 startPos, FWaterDisplacement hookObject)
    {
        yield return null;
        if (fishPopulation.Length != 0)
        {
            int num = UnityEngine.Random.Range(0, fishPopulation.Length);
            float num2 = RandWeight(new Vector2(fishPopulation[num].weightRange.x, fishPopulation[num].weightRange.y));
            try
            {
                var path = GameFish.Get(t => t.speciesName == fishPopulation[num].FishSpecies).GetFishModel(num2);
                FFish component = Instantiate(path, SceneSettings.Instance.transform).GetComponent<FFish>();
                if (component == null)
                {
                    int i = 0;
                }

                component.fFishSystem = fFishSystem;
                component.fishWeight = num2;
                component.fishSpecies = fishPopulation[num].FishSpecies;
                component.transform.localScale = GameFish.Get(t => t.speciesName == fishPopulation[num].FishSpecies)
                    .GetFishScale(num2);
                component.transform.position = startPos;
                component.currentFeedingZone = this;
                fishesOnZone.Add(component);
                component.EnableFish();
                fFishSystem.inwaterFishObjectsSpawner.Add(component);
                if ((bool)hookObject)
                {
                    hookObject.fishListCreated.Add(component);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }

    public IEnumerator GenerateFishPopulationOnStart()
    {
        yield return null;
        for (int i = 0; i < fishPopulation.Length; i++)
        {
            for (int j = 0; j < fishPopulation[i].AmountPopulationZone; j++)
            {
                Vector3 position = transform.position + UnityEngine.Random.insideUnitSphere * rangeZone;
                position.y = transform.position.y - 0.2f;
                float num = RandWeight(new Vector2(fishPopulation[i].weightRange.x, fishPopulation[i].weightRange.y));
                FFish component = Instantiate(
                    GameFish.Get(t => t.speciesName == fishPopulation[i].FishSpecies).GetFishModel(num),
                    SceneSettings.Instance.transform).GetComponent<FFish>();
                component.fFishSystem = fFishSystem;
                component.fishWeight = num;
                component.fishSpecies = fishPopulation[i].FishSpecies;
                component.transform.localScale = GameFish.Get(t => t.speciesName == fishPopulation[i].FishSpecies)
                    .GetFishScale(num);
                component.transform.position = position;
                component.currentFeedingZone = this;
                fishesOnZone.Add(component);
                yield return null;
            }
        }
    }

    private void checkAndUpdateFishPopulation()
    {
        for (int i = 0; i < fishesOnZone.Count; i++)
        {
            if (fishesOnZone[i] == null)
            {
                int num = UnityEngine.Random.Range(0, fishPopulation.Length - 1);
                if (fishPopulation[num].AmountPopulationZone > fishesOnZone.Count)
                {
                    break;
                }

                Vector3 position = transform.position + UnityEngine.Random.insideUnitSphere * 1f;
                position.y = transform.position.y - 0.2f;
                float num2 =
                    RandWeight(new Vector2(fishPopulation[num].weightRange.x, fishPopulation[num].weightRange.y));
                FFish component = Instantiate(
                    GameFish.Get(t => t.speciesName == fishPopulation[num].FishSpecies).GetFishModel(num2),
                    SceneSettings.Instance.transform).GetComponent<FFish>();
                component.fFishSystem = fFishSystem;
                component.fishWeight = num2;
                component.fishSpecies = fishPopulation[num].FishSpecies;
                component.transform.localScale = GameFish.Get(t => t.speciesName == fishPopulation[num].FishSpecies)
                    .GetFishScale(num2);
                component.transform.position = position;
                component.currentFeedingZone = this;
                fishesOnZone[i] = component;
                Debug.Log("Replace fish " + component.name);
            }
        }
    }

    private float RandWeight(Vector2 range)
    {
        float result = range.x;
        for (int i = 0; (float)i < 10f; i++)
        {
            result = UnityEngine.Random.Range(range.x, range.y);
        }

        return result;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.DrawWireSphere(transform.position, rangeZone);
    }
}