﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneController : MonoBehaviour
{
    [SerializeField]
    Transform player;

    [SerializeField]
    List<ScenePlatformView> platformViews;

    [SerializeField]
    List<SceneObjectItem> sceneObjectItems;

    SceneRenderArea renderArea;

    List<GameObject> currentPlatformViews;

    // Start is called before the first frame update
    void Start()
    {
        SetRenderArea();
        currentPlatformViews = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        UpdateEnvironment();
    }

    void SetRenderArea()
    {
        float height = 2f * Camera.main.orthographicSize;
        float width = height * Camera.main.aspect;

        renderArea = new SceneRenderArea(width);
    }

    void UpdateEnvironment()
    {
        float newPlatformPosition = 0;
        bool newPlatformNeeded = true;

        List<GameObject> viewsToRemove = new List<GameObject>();

        // Update my render min max position based on player's position
        renderArea.UpdateRenderArea(player.position);

        foreach(GameObject goPlatformView in currentPlatformViews)
        {
            ScenePlatformViewBounds platformViewBounds = goPlatformView.GetComponent<ScenePlatformViewBounds>();

            // No more new platforms needed
            if (platformViewBounds.MinX > renderArea.MaxX)
                newPlatformNeeded = false;

            // Remove unused platforms - player way ahead
            if (platformViewBounds.MaxX < renderArea.MinX)
                viewsToRemove.Add(goPlatformView);

            // Set new Position
            float xPos = platformViewBounds.MaxX + platformViewBounds.Width * 0.5f;

            newPlatformPosition = Mathf.Max(newPlatformPosition, xPos);

        }

        // Remove unused platforms
        RemoveUnusedPlatforms(viewsToRemove);

        // If new platforms needed, add them
        if (newPlatformNeeded) //  && count <= 3
            AddNewPlatform(newPlatformPosition);
    }

    void RemoveUnusedPlatforms(List<GameObject> unusedPlatforms)
    {
        foreach (GameObject go in unusedPlatforms)
        {
            currentPlatformViews.Remove(go);
            Destroy(go);
        }
    }

    void AddNewPlatform(float xPosition)
    {
        ScenePlatformView platformView;
        int randomIndex = 0;

        if (platformViews.Count > 1)
            randomIndex = Random.Range(0, platformViews.Count);

        platformView = platformViews[randomIndex];

        // Create new game object
        GameObject newPlatformView = platformView.CreateItem(new Vector3(xPosition, 0, 0));

        // Add the new platform to the current list
        currentPlatformViews.Add(newPlatformView);

        // Add scene object items to platform
        ScenePlatformViewBounds platformViewBounds = newPlatformView.GetComponent<ScenePlatformViewBounds>();

        AddSceneObjectItem(platformViewBounds.GetBounds());
    }

    void AddSceneObjectItem(Bounds bounds)
    {
        foreach(SceneObjectItem sceneObject in sceneObjectItems)
        {
            int num = Random.Range(1, sceneObject.MaxNumber + 1);
            for (int i = 0; i < num; i++) {
                GameObject newObjectItem = sceneObject.CreateItem(bounds);


            }
        }
    }
}
