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

public enum coloringSchemaType
{
    two_color_smooth,
    rainbow_color,
    rainbow_color_smooth,
    precision_navigation
};

public class terrainSettings : MonoBehaviour
{
	public bool hideArea = true;
    public coloringSchemaType coloringSchema;

    public bool showContourLines = false;
    public float contourStepInM = 10.0f;
    public float contourWidth = 1.0f;

    public float precisionNavUpperDepth = 5.0f;
    public float precisionNavBottomDepth = 10.0f;

    private Material terrainMaterial = null;
    private bool firstValidation = true;

    public Vector3 originalPosition = new Vector3(0.0f, 0.0f, 0.0f);

    public void testFunction()
    {
        Debug.Log("YES");
    }

    void Awake()
    {
        //Debug.Log("Awake");
    }

    void Start()
    {
    }

    void Update()
    {
    }

    void OnGUI()
    {
    }

    public void OnValidate()
    {
        if (firstValidation)
        {
            //Debug.Log("OnValidate + firstValidation");
            firstValidation = false;
            terrainMaterial = Resources.Load<Material>("Bag Loader/Materials/standardMaterial");

            hideArea = gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("hideNoData") == 0.0f ? false : true;
            coloringSchema = (coloringSchemaType)(int)gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("coloringSchema");

            showContourLines = gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("showContourLines") == 0.0f ? false : true;
            contourStepInM = 1.0f / gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("contourStepSize");
            contourWidth = gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("contourWidth");

            precisionNavUpperDepth = (int)gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("precisionNavUpperDepth");
            precisionNavBottomDepth = (int)gameObject.GetComponent<Terrain>().materialTemplate.GetFloat("precisionNavBottomDepth");
        }

        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("zeroLevel", gameObject.GetComponent<Terrain>().terrainData.heightmapScale.y);

        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("hideNoData", hideArea ? 1.0f : 0.0f);
        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("coloringSchema", (int)coloringSchema);

        // ********* ContourLines *********
        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("showContourLines", showContourLines ? 1.0f : 0.0f);

        if (contourStepInM < 0.1f)
            contourStepInM = 0.1f;
        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("contourStepSize", 1.0f / contourStepInM);

        if (contourWidth < 0.1f)
            contourWidth = 0.1f;
        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("contourWidth", contourWidth);

        // ********* PrecisionNav *********
        float newPrecisionNavUpperDepth = precisionNavUpperDepth;
        float newPrecisionNavBottomDepth = precisionNavBottomDepth;
        if (newPrecisionNavBottomDepth < newPrecisionNavUpperDepth)
        {
            newPrecisionNavBottomDepth = newPrecisionNavUpperDepth + 0.1f;
        }

        newPrecisionNavBottomDepth = precisionNavBottomDepth;
        if (newPrecisionNavUpperDepth > newPrecisionNavBottomDepth)
        {
            newPrecisionNavUpperDepth = newPrecisionNavBottomDepth - 0.1f;
        }

        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("precisionNavUpperDepth", newPrecisionNavUpperDepth);
        precisionNavUpperDepth = newPrecisionNavUpperDepth;
        gameObject.GetComponent<Terrain>().materialTemplate.SetFloat("precisionNavBottomDepth", newPrecisionNavBottomDepth);
        precisionNavBottomDepth = newPrecisionNavBottomDepth;
    }
}
