using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.IO;
using UTILS;
using System.Text;

namespace LFSLapper
{
    partial class LFSClient
    {
        public void CreateFile(GLScript.unionVal val, infoPlayer currInfoPlayer, string function, string FileName, string Folder, string FileExistTodo)
        {
            string Path = "";
            string BackUpPath = "";

            try
            {
                Path = @"" + Folder + "\\" + FileName + ".txt";
                BackUpPath = @"" + Folder + "\\" + FileName + "_Backup.txt";

                FileInfo File = new FileInfo(Path);
                if (File.Exists)
                {
                    switch (FileExistTodo)
                    {
                        case "0": //do nothing
                            {

                            }
                            break;
                        case "1": //overwrite
                            {
                                File.Delete(); //Delete current file
                                File.Create().Close(); //Create new file
                            }
                            break;
                        case "2": //overwrite + create backup
                            {
                                try
                                {
                                    // Move file with a new name within the same directory.
                                    File.MoveTo(BackUpPath);
                                    //Create new file.
                                    FileInfo NewFile = new FileInfo(Path);
                                    NewFile.Create().Close();
                                }
                                catch
                                {
                                    myDebug.WriteLine("err", "ERROR on Function: " + function + ", backup file exist , cannot create new backup file!");
                                }
                            }
                            break;
                    }
                }
                else
                {
                    File.Create().Close();
                }
            }
            catch
            {
                myDebug.WriteLine("err","^3WARNING: ^8on Function: " + function + ", path is not correct: " + Path + "");
                Console.WriteLine("^3WARNING: ^8on Function: " + function + ", path is not correct: " + Path + "");
            }
        }

        public void DeleteFile(GLScript.unionVal val, infoPlayer currInfoPlayer, string function, string FileName, string Folder, string CopyFolder, string Extension)
        {
            //Set Path and Path to copy to.
            string Path = @"" + Folder + "\\" + FileName + "" + Extension;
            string CopyPath = @"" + CopyFolder + "\\" + FileName + "" + Extension;

            try
            {
                FileInfo File = new FileInfo(Path);
                //verify file path
                if (File.Exists)
                {
                    if (CopyFolder != "")
                    {
                        FileInfo CopyFile = new FileInfo(CopyPath);
                        CopyFile.Delete();
                        File.CopyTo(CopyPath);
                    }
                    //if orginal file exist     
                    File.Delete();
                }
                else
                {
                    myDebug.WriteLine("err", "^3WARNING: ^8on Function: " + function + ", File doesnt exist: " + Path + "");
                }
            }
            catch
            {
            }
        }

        public void EditFile(GLScript.unionVal val, infoPlayer currInfoPlayer, string function, string FileName, string Folder, string Text, string LineToEdit, string Extension)
        {
            string Path = @"" + Folder + "\\" + FileName + "" + Extension;
            if (File.Exists(Path))
            {
                try
                {
                    //Convert string to int.
                    int EditLine = Int32.Parse(LineToEdit);
                    //Read file.
                    if (EditLine == -1)
                    {
                        //enable AppendText, opens file 
                        TextWriter WriteFile = new StreamWriter(Path, true);
                        WriteFile.WriteLine(Text);
                        WriteFile.Close(); //close file after writing
                    }
                    else
                    {
                        string[] ArrayOfLines = System.IO.File.ReadAllLines(Path);
                        if (ArrayOfLines.Length >= 1)
                        {
                            if ((EditLine - 1) < ArrayOfLines.Length)
                            {
                                if ((val.ArgsVals[2] as GLScript.Parseur).val.typVal.ToString() == "str")
                                {
                                    ArrayOfLines[EditLine - 1] = Text;

                                    StreamWriter WriteFile = new StreamWriter(Path);
                                    for (int x = 0; x < ArrayOfLines.Length; x++)
                                    {
                                        WriteFile.WriteLine(ArrayOfLines[x]);
                                    }
                                    //close the file after writing.
                                    WriteFile.Close();
                                }
                                else
                                {
                                    if (Text.ToString() == "-1")
                                    {
                                        // delete line if input is Text = -1;
                                        StreamWriter WriteFile = new StreamWriter(Path);
                                        for (int x = 0; x < ArrayOfLines.Length; x++)
                                        {
                                            if (x == EditLine - 1)
                                                continue;
                                            //Write the entire array back to the file.
                                            WriteFile.WriteLine(ArrayOfLines[x]);
                                        }
                                        //close the file after writing.
                                        WriteFile.Close();
                                    }
                                    else
                                    {
                                        throw new GLScript.GLApp.GLScriptException("ERROR on Function: '" + function + "', third argument has to be a text string or number -1.");
                                    }
                                }
                            }
                            else
                            {
                                throw new GLScript.GLApp.GLScriptException("ERROR on Function: '" + function + "', File doesnt contain that many lines [" + EditLine + "/" + ArrayOfLines.Length + "]");
                            }
                        }
                        else
                        {
                            throw new GLScript.GLApp.GLScriptException("ERROR on Function: '" + function + "', File is empty: [" + ArrayOfLines.Length + "] lines detected, you cant edit any lines.");
                        }
                    }
                }
                catch(Exception ex)
                {
                    throw new GLScript.GLApp.GLScriptException("ERROR on Function: '" + function + "', " + ex.Message);
                }
            }
            else
            {
                throw new GLScript.GLApp.GLScriptException("ERROR on Function: '" + function + "', File doesnt exist");
            }
        }
    }
}
