using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;

namespace CobXmlSupport
{
    public class codeCompleter
    {
        private readonly string LocBackup = "C:\\CCBackup\\";

        private Dictionary<string, string> __wrkDicIn = null;
        private Dictionary<string, string> __wrkDicOut = null;
        private string __FieldPrefix;
        private string __filePathId;
        private string __fileId;
        private string __pathId;
        private string __jsonPathId;
        private string __ccFileInId;
        private string __ccFileOutId;
        private string __ccFileInIdNoSuff;
        private string __ccFileOutIdNoSuff;
        public readonly string JSON = ".json";

        public codeCompleter()
        {
            __wrkDicIn = null;
            __wrkDicOut = null;
            __FieldPrefix = "";
            __filePathId = "";
            __fileId = "";
            __pathId = "";
            __jsonPathId = "";
            __ccFileInId = "";
            __ccFileOutId = "";
            __ccFileInIdNoSuff = "";
            __ccFileOutIdNoSuff = "";
        }

        public codeCompleter(string pathFileId, string fpref)
        {
            __FieldPrefix = fpref;
            __filePathId = pathFileId;
            nameCreator(__filePathId);
            __wrkDicIn = new Dictionary<string, string>();
            __wrkDicOut = new Dictionary<string, string>();
        }

        public string jsonPathId
        {
            get
            {
                return __jsonPathId;
            }

        }

        public string FilePathId
        {
            set
            {
                __filePathId = value;
                nameCreator(__filePathId);
            }
            get { return __filePathId; }
        }

        public string FileId
        {
            get { return __fileId; }
        }

        public string PathId
        {
            get { return __pathId; }
        }
        public string CcFileInId
        {
            get { return __ccFileInId; }
        }
        public string CcFileOutId
        {
            get { return __ccFileOutId; }
        }

        public Dictionary<string, string> WorkDicIn
        {
            get { return __wrkDicIn; }
            set { __wrkDicIn = value; }
        }

        public Dictionary<string, string> WorkDicOut
        {
            get { return __wrkDicOut; }
            set { __wrkDicOut = value; }
        }

        private void nameCreator(string inPathFname)
        {
            string gPrefix = "." + __FieldPrefix.Replace("_", "").Trim();

            __fileId = inPathFname.Substring(inPathFname.LastIndexOf("\\") + 1);
            __pathId = inPathFname.Substring(0, inPathFname.LastIndexOf("\\") + 1);
            if (Directory.Exists(__pathId + @"JsonExt\")) { }
            else
            {
                Directory.CreateDirectory(__pathId + @"JsonExt\");
            }
            __jsonPathId = __pathId + @"JsonExt\";
            __ccFileInId = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".In" + gPrefix + JSON;
            __ccFileOutId = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".Out" + gPrefix + JSON;
            __ccFileInIdNoSuff = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".In" + gPrefix;
            __ccFileOutIdNoSuff = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".Out" + gPrefix;
        }

        private string backupSuffix(bool rideOver=false)
        {
            string tmpSuff = "";
            if (S.ettingUserCodeBcup||rideOver) 
            {
                tmpSuff = "." +
                    (DateTime.Now.Year - 2000).ToString("00") +
                    DateTime.Now.Month.ToString("00") +
                    DateTime.Now.Day.ToString("00") + "T" +
                    DateTime.Now.Hour.ToString("00") +
                    DateTime.Now.Minute.ToString("00") +
                    DateTime.Now.Second.ToString("00") + JSON;
            }
            else
            {
                tmpSuff = ".backup" + JSON;
            }
            return tmpSuff;
        }

        private string SafeBackPath()
        {
            return LocBackup + 
                "BC"+
                (DateTime.Now.Year - 2000).ToString("00") +
                DateTime.Now.Month.ToString("00") +
                DateTime.Now.Day.ToString("00") +
                "\\";
        }



        public void TryFill_In_Dic()
        {
            string ccFileInId = __jsonPathId + __ccFileInId;
            if (__filePathId.Trim() == "") { }
            else
            {
                if (File.Exists(ccFileInId))
                {
                    string fileStr = File.ReadAllText(ccFileInId);
                    moveJsonFieldDictionary(fileStr, "IN");
                }
            }
        }

        public void TryFill_Out_Dic()
        {
            string ccFileOutId = __jsonPathId + __ccFileOutId;
            if (__filePathId.Trim() == "") { }
            else
            {
                if (File.Exists(ccFileOutId))
                {
                    string fileStr = File.ReadAllText(ccFileOutId);
                    moveJsonFieldDictionary(fileStr, "OUT");
                }
            }
        }

        private void moveJsonFieldDictionary(string fString, string inOut)
        {
            if (inOut == "IN")
                __wrkDicIn = JsonConvert.DeserializeObject<Dictionary<string, string>>(fString);
            else
                __wrkDicOut = JsonConvert.DeserializeObject<Dictionary<string, string>>(fString);
        }

        public void Save_In_Dic()
        {
            string ccFileInId = __jsonPathId + __ccFileInId;
            if (Directory.Exists(__jsonPathId + @"JsonBackup\")) { }
            else
            {
                Directory.CreateDirectory(__jsonPathId + @"JsonBackup\");
            }

            //Panik-backup
            if (Directory.Exists(SafeBackPath())) { }
            else
            {
                Directory.CreateDirectory(SafeBackPath());
            }

            string ccFileInPanikBackup = SafeBackPath() + __ccFileInIdNoSuff + backupSuffix(true);

            //Panik-backup

            string ccFileInBackup = __jsonPathId + @"JsonBackup\" + __ccFileInIdNoSuff + backupSuffix();

            if (__filePathId.Trim() == "") { }
            else
            {
                if (File.Exists(ccFileInId))
                {
                    File.Copy(ccFileInId, ccFileInPanikBackup);
                    if (File.Exists(ccFileInBackup))
                    {
                        File.Delete(ccFileInBackup);
                    }
                    File.Move(ccFileInId, ccFileInBackup);
                }
                File.WriteAllText(ccFileInId, createJsonFromDic("IN"));
            }
        }


        public void Save_Out_Dic()
        {
            string ccFileOutId = __jsonPathId + __ccFileOutId;
            if (Directory.Exists(__jsonPathId + @"JsonBackup\")) { }
            else
            {
                Directory.CreateDirectory(__jsonPathId + @"JsonBackup\");
            }

            //Panik-backup
            if (Directory.Exists(SafeBackPath())) { }
            else
            {
                Directory.CreateDirectory(SafeBackPath());
            }

            string ccFileOutPanikBackup = SafeBackPath() + __ccFileOutIdNoSuff + backupSuffix(true);

            //Panik-backup

            string ccFileOutBackup = __jsonPathId + @"JsonBackup\" + __ccFileOutIdNoSuff + backupSuffix();

            if (__filePathId.Trim() == "") { }
            else
            {
                if (File.Exists(ccFileOutId))
                {
                    File.Copy(ccFileOutId, ccFileOutPanikBackup);

                    if (File.Exists(ccFileOutBackup))
                    {
                        File.Delete(ccFileOutBackup);
                    }
                    File.Move(ccFileOutId, ccFileOutBackup);
                }
                File.WriteAllText(ccFileOutId, createJsonFromDic("OUT"));
            }
        }

        private string createJsonFromDic(string inOut)
        {
            string tmpJson = "";
            if (inOut == "IN")
                tmpJson = JsonConvert.SerializeObject(__wrkDicIn);
            else
                tmpJson = JsonConvert.SerializeObject(__wrkDicOut);
            return tmpJson;
        }
    }
}
