using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.RenderPass;
using UnityEngine;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;

namespace Assets
{
    [RequireComponent(typeof(MeshFilter))]
    [RequireComponent(typeof(MeshRenderer))]
    [RequireComponent(typeof(LandmassGenerator))]
    public class MapRenderer : MonoBehaviour
    {
        private bool _rerender = false;

        public GameObject citiesGameObject;
        public GameObject forestGameObject;
        public GameObject[] trees;
        
        private IList<IRenderer> Renderers => new List<IRenderer>
        {
            new LandmassRenderer(),
            new CityRenderer(citiesGameObject),
            new ForestRender
            {
                ForestObject = forestGameObject,
                Trees = trees,
            }
        };
        
        public void GenerateRandom()
        {
            var generator = GetComponent<LandmassGenerator>();

            generator.seed = Environment.TickCount;
            generator.Generate(map => _rerender = true);
        }

        private void Update()
        {
            if (_rerender)
            {
                Render();
                _rerender = false;
            }
        }

        public void Render()
        {
            var map = GetComponent<LandmassGenerator>().Map;

            if (map == null)
                return;
            
            foreach (var current in Renderers)
                current.Render(map, this);
        }

        public void OnDrawGizmos()
        {
            var map = GetComponent<LandmassGenerator>().Map;

            if (map == null)
                return;
            
            foreach (var current in Renderers)
                current.DrawGizmos(map, this);
        }
    }
}