using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using compileApplications;
using System.Text;
using System.Timers;
using System.Diagnostics;
using System;
using UnityEditor;
using System.IO;
using UnityEngine.Events;
using System.Data;

namespace compileApplications
{

    public class SubmitCode : MonoBehaviour
    {
        public Button btnSubmit;
        public Button btnStop;
        public Button btnRun;

        public TMP_InputField inputCode;
        public TMP_InputField testCases;
        public TextMeshProUGUI errorOutput;

        public TextMeshProUGUI consoleOutput;
        public TextMeshProUGUI currentTestCase;
        public TextMeshProUGUI userReturn;
        public TextMeshProUGUI expectedReturn;
        public TextMeshProUGUI testCaseCompletion;
        public TextMeshProUGUI percentCompletion;
        public TextMeshProUGUI passOrFail;

        public Image progressBar;
        public GameObject outputConsole;

        public static string pathToDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).Replace("\\", "/");
        public static string fileName;
        private static string extension;
        private static string path;
        private static string pathToTestCase;
        private static string fullPath;
        private static string nameOfFile;
        private static string language;
        private Action storeCompilerOption { get; set; }



        public static void SetValuesFromDB()
        {
            language = GetUserJobData.userDataRow[4].ToString();

            SubmitCode submitCode = new SubmitCode();
            submitCode.ConfirmLanguage();
        }

        public void Start()
        {
            //btnSubmit.onClick.AddListener(InputHandlerCSharp);
            //UnityEngine.Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
            btnRun.onClick.AddListener(InputHandlerRun);
            btnSubmit.onClick.AddListener(InputHandlerSubmit);
            btnStop.onClick.AddListener(endProcess);
            inputCode.onEndEdit.AddListener(WriteCodeText);
            testCases.onEndEdit.AddListener(WriteTestCasesText);

        }
        public void ConfirmLanguage()
        {
            language = GetUserJobData.userDataRow[4].ToString();
            /*nameOfFile = "algorithm";
            extension = ".txt";
            fileName = string.Format("{0}{1}", nameOfFile, extension);*/
            fullPath = string.Format(@"{0}{1}", pathToDocuments, GetUserJobData.userDataRow[7].ToString());
            pathToTestCase = string.Format(@"{0}{1}", pathToDocuments, GetUserJobData.userDataRow[9].ToString());

            ReadCodeText(inputCode);
            ReadTestCasesText(testCases);

            if (language == "C#")
            {
               /* nameOfFile = "algorithm";
                extension = ".txt";
                fileName = string.Format("{0}{1}", nameOfFile, extension);
                path = string.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserSavedCode\C#\", pathToDocuments);
                pathToTestCase = string.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserTestCases\{1}\", pathToDocuments, language);*/
                storeCompilerOption = InputHandlerCSharp;



            }
            else if (language == "C++")
            {
              /*  nameOfFile = "algorithm";
                extension = ".cpp";
                fileName = String.Format("{0}{1}", nameOfFile, extension);
                path = String.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserSavedCode\Cpp\", pathToDocuments);
                pathToTestCase = string.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserTestCases\{1}\", pathToDocuments, language);*/

                storeCompilerOption = InputHandlerCPP;



            }
            else if (language == "Java")
            {/*
                nameOfFile = "algorithm";
                extension = ".java";
                fileName = String.Format("{0}{1}", nameOfFile, extension);
                path = String.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserSavedCode\Java\", pathToDocuments);
                pathToTestCase = string.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserTestCases\{1}\", pathToDocuments, language);*/
                storeCompilerOption = InputHandlerJava;



            }
            else if (language == "Python")
            {
               /* nameOfFile = "algorithm";
                extension = ".py";
                fileName = String.Format("{0}{1}", nameOfFile, extension);
                path = String.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserSavedCode\Python\", pathToDocuments);
                pathToTestCase = string.Format(@"{0}\Unity\AlgoGame\Assets\Resources\Jobs\Algorithms\User\UserTestCases\{1}\", pathToDocuments, language);*/
                storeCompilerOption = InputHandlerPython;
               /* ReadCodeText(inputCode);
                ReadTestCasesText(testCases);*/


            }

        }
        public void InputHandlerRun()
        {
            //btnRun.gameObject.SetActive(false);
            //btnSubmit.gameObject.SetActive(false);

            consoleOutput.SetText("");
            errorOutput.SetText("");
            storeCompilerOption();
        }

        public void InputHandlerSubmit()
        {
            //btnSubmit.gameObject.SetActive(false);
            //consoleOutput.SetText("");
            //errorOutput.SetText("");
            //storeCompilerOption();
        }

        public void InputHandlerCSharp()
        {
            CompileJob.JobCompileCSharp(inputCode.text, path, nameOfFile, errorOutput, consoleOutput, outputConsole);
        }

        public void InputHandlerPython()
        {
            CompileJob.JobCompilePython(path, nameOfFile, errorOutput, consoleOutput, outputConsole, userReturn, expectedReturn, currentTestCase, passOrFail);
        }

        public void InputHandlerCPP()
        {
            CompileJob.JobCompileCPP(path, nameOfFile, errorOutput, consoleOutput, outputConsole);
        }

        public void InputHandlerJava()
        {
            CompileJob.JobCompileJava(path, nameOfFile, errorOutput, consoleOutput, outputConsole);
        }

        [MenuItem("Tools/Write file")]
        public void WriteCodeText(string codeText)
        {

            using (StreamWriter writer = new StreamWriter(fullPath, false))
            {
                writer.Write(codeText);
                AssetDatabase.ImportAsset(fullPath);
                TextAsset asset = (TextAsset)Resources.Load(nameOfFile);
            }
        }

        public void WriteTestCasesText(string testCases)
        {

            using (StreamWriter writer = new StreamWriter(pathToTestCase, false))
            {
                writer.Write(testCases);
                AssetDatabase.ImportAsset(pathToTestCase);
                TextAsset asset = (TextAsset)Resources.Load(nameOfFile);
            }
        }

        [MenuItem("Tools/Read file")]
        public void ReadCodeText(TMP_InputField inputCode)
        {
            //UnityEngine.Debug.Log("\"" + fullPath + "\"");
            using (StreamReader reader = new StreamReader(fullPath))
            {
                UnityEngine.Debug.Log(testCases);
                UnityEngine.Debug.Log(reader);
                inputCode.text = reader.ReadToEnd();

            }
        }

        public void ReadTestCasesText(TMP_InputField testCases)
        {
            using (StreamReader reader = new StreamReader(pathToTestCase))
            {
                testCases.text = reader.ReadToEnd();
            }
        }

        public void endProcess()
        {
            JobData.InsertJobData();
            
            /*try
            {
                if (language == "C#")
                {
                    CompileProgram.executeCS.Kill();
                }
                else if (language == "C++")
                {
                    CompileProgram.executeCPP.Kill();
                }
                else if (language == "Java")
                {
                    CompileProgram.executeJava.Kill();
                }
                else if (language == "Python")
                {
                    CompileProgram.executePython.Kill();
                }
                errorOutput.text = "Process ended";
                outputConsole.SetActive(false);
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.Log(ex);
            }*/
        }
    }
}