using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System;

/**
 
{ "Database" :	{ "ClientConnection" : string,
                  "DatabaseName" : string ,
                  "DBCollection" : string
                },
    "Kspeedcontroller" : 0.4,
    "SimulationTime" : 100.0,
    "VelocitySetpointGenerator" :  { "Dark" : 0.6,
                                     "Bright" : 1.0,
                                     "Threshold1" : 0.78,
                                     "Threshold2" : 0.83,
                                     "DistanceCenterToWheel" : 0.07,
                                     "K" : 2,
                                     "ForwardSpeed" : 0.1
                                   },
	"Parcours" : {
		"LineWidth" : 0.1, 
		"Kind" : "PieceWise",	
		"LinePieces" : [
			{ "x" : 1.1, "y" : 12.2},
			{ "x" : 1.43, "y" : 4.5},
			{ "x" : 1.8, "y" : 3.5},
			{ "x" : 1.1, "y" : 2.5}
			]
	}
} **/

[System.Serializable]
public class ConfigDatabase
{
    public string ClientConnection;
    public string DatabaseName;
    public string DBCollection;
}

[System.Serializable]
public class ConfigVelocitySetpointGenerator
{
    public float Dark;
    public float Bright;
    public float Threshold1;
    public float Threshold2;
    public float DistanceCenterToWheel;
    public float K;
    public float ForwardSpeed;
}

[System.Serializable]
public class ConfigLinePieces
{
    public float x;
    public float y;
}

[System.Serializable]
public class ConfigParcours
{
    public float LineWidth;
    public string Kind; // PieceWise of Trail
    public ConfigLinePieces[] LinePieces;
}

[System.Serializable]
public class Config
{
    public ConfigDatabase Database;
    public float Kspeedcontroller;
    public float SimulationTime;
    public ConfigVelocitySetpointGenerator VelocitySetpointGenerator;
    public ConfigParcours Parcours;
}

public class ConfigReader : MonoBehaviour
{
    public Config configuration;
    //private DatabaseConnection DatabaseConnection;

    void Awake()  //before start
    {
        ReadConfig();
        if (DatabaseGivenInFile())
        {
            DatabaseConnectionCreate();
        }
        else
        {
            Debug.LogWarning("Database-credentials not given in config-file.");
        }
    }

    private void ReadConfig()
    {
        string curdir = Directory.GetCurrentDirectory(); // current directory of executable. The config-file should be there
        StreamReader reader = new StreamReader(curdir + "/config.json");
        string json_string = reader.ReadToEnd();
        configuration = JsonUtility.FromJson<Config>(json_string);
        //Debug.Log(json_string);
    }

    /*public DatabaseConnection GetConnection()
    {
        return DatabaseConnection;
    }*/

    private void Start()
    {
        if (DatabaseGivenInFile())
        {
            /* DatabaseConnection.Connect(); /* Scriptable Object DataConnection must already be defind in Awake() */ 
        }
    }

    private void DatabaseConnectionCreate()
    {
        /*DatabaseConnection = ScriptableObject.CreateInstance<DatabaseConnection>();
        DatabaseConnection.ClientConnection = configuration.Database.ClientConnection;
        DatabaseConnection.DatabaseName = configuration.Database.DatabaseName;
        DBCollection Collection = ScriptableObject.CreateInstance<DBCollection>(); // collection to use with config file
        Collection.name = configuration.Database.DBCollection;
        DatabaseConnection.mycollection = Collection;*/
    }

    public bool DatabaseGivenInFile()
    {
        if (String.IsNullOrEmpty(configuration.Database.ClientConnection)) return false;
        if (String.IsNullOrEmpty(configuration.Database.DatabaseName)) return false;
        if (String.IsNullOrEmpty(configuration.Database.DBCollection)) return false;
        return true;
    }

    public bool VelocitySetpointGeneratorAvailable()
    {
        if (float.IsNaN(configuration.VelocitySetpointGenerator.Bright)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.Dark)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.DistanceCenterToWheel)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.ForwardSpeed)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.K)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.Threshold1)) return false;
        if (float.IsNaN(configuration.VelocitySetpointGenerator.Threshold2)) return false;
        return true;
    }

    public bool ValidParcours()
    {
        if (configuration.Parcours.LinePieces.Length > 1)
        {
            if (configuration.Parcours.Kind == "PieceWise")
            {
                // There should be an even amount of points
                if (configuration.Parcours.LinePieces.Length % 2 == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }
    

}
