using System.Collections;
using System.Collections.Generic;
using Assets.AnnotationPass;
using Assets.Common;
using UnityEngine;
using Random = System.Random;
using Tree = Assets.Common.Tree;

namespace Assets.RenderPass
{
    public class ForestRender : IRenderer
    {
        public ICollection<GameObject> Trees { get; set; }
        public GameObject ForestObject { get; set; }
        
        public void Render(Map map, Component component)
        {
            var rng = new Random(map.Seed);
            var trees = map.GetProperty<IEnumerable<Tree>>(ForestPass.TreesProperty);

            var toRemove = new List<GameObject>();
            foreach (Transform child in ForestObject.transform)
                toRemove.Add(child.gameObject);

            foreach (var o in toRemove)
                Object.DestroyImmediate(o);
            
            foreach (var tree in trees)
            {
                var origin = tree.Placement.ToVector3() + (float)tree.Site.GetProperty<double>(CommonSiteProperties.Height) * Vector3.up;
                Object.Instantiate(GetRandomTree(rng), origin, Quaternion.identity, ForestObject.transform);
            }
        }

        private GameObject GetRandomTree(Random generator)
        {
            return Trees.RandomElement(generator);
        }

        public void DrawGizmos(Map map, Component component)
        {
            
        }
    }
}