using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ini.Net;

namespace GcDevicePc.Common
{
    class RunTableHelper
    {
        private string _filename;

        private string _asfilename;

        public bool fileflag =false;

        private IniFile RunTableFile;
        private IniFile AsRunTableFile;

        private string batname = "Default";

        public Int32 TotalNum = 0;
        public List<String> _methodname = new List<String>();
        public List<Int32> _runcount = new List<Int32>();
        public List<Int32> _runtime = new List<Int32>();
        public List<Int32> _rundelay = new List<Int32>();

        private void RunTableInit()
        {
            _methodname.Add("开机");
            _runcount.Add(1);
            _runtime.Add(1);
            _rundelay.Add(1);
        }

        public void setBatName(String name)
        {
            this.batname = name;
        }

        public RunTableHelper(String folder, String filename)
        {
            this.batname = filename;
            _filename = folder+"\\"+filename;

            try
            {
                if (!File.Exists(_filename))
                {
                    StreamWriter sw = new StreamWriter(_filename, false, Encoding.Unicode);
                    sw.WriteLine("[Bat]");
                    sw.WriteLine("[Total]");
                    sw.WriteLine("[运行表]");
                    sw.WriteLine("[循环]");
                    sw.WriteLine("[运行时间]");
                    sw.WriteLine("[间隔时间]");
                    sw.Close();
                    fileflag = true;
                    RunTableInit();
                }
                else
                {
                    fileflag = true;
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }

            if (fileflag)
                LoadRunTable();
        }

        public void LoadRunTable()
        {
            RunTableFile = new IniFile(_filename);
            //int num = 0;
            int i = 0;
            if (RunTableFile.KeyExists("Total", "count"))
            {
                this.TotalNum = RunTableFile.ReadInteger("Total", "count");
                for (i = 0; i < TotalNum; i++)
                {
                    _methodname.Add(RunTableFile.ReadString("运行表",i.ToString()));
                    _runcount.Add(RunTableFile.ReadInteger("循环", i.ToString()));
                    _runtime.Add(RunTableFile.ReadInteger("运行时间", i.ToString()));
                    _rundelay.Add(RunTableFile.ReadInteger("间隔时间", i.ToString()));
                }
            }
            else
            {
                for (i = 0; i < 100; i++)
                {
                    if ( RunTableFile.KeyExists("运行表", i.ToString()) && RunTableFile.KeyExists("循环", i.ToString()) && RunTableFile.KeyExists("运行时间", i.ToString()))
                    {
                        _methodname.Add(RunTableFile.ReadString("运行表", i.ToString()));
                        _runcount.Add(RunTableFile.ReadInteger("循环", i.ToString()));
                        _runtime.Add(RunTableFile.ReadInteger("运行时间", i.ToString()));
                        _rundelay.Add(RunTableFile.ReadInteger("间隔时间", i.ToString()));
                    }
                    else
                    {
                        RunTableFile.WriteInteger("Total", "count", i);
                        break;
                    }

                }
            }

        }

        public void DelRunTable()
        {
            if(File.Exists(_filename))
                File.Delete(_filename);
        }

        public void AddItem(String name, Int32 count, Int32 time, Int32 delay)
        {
            _methodname.Add(name);
            _runcount.Add(count);
            _runtime.Add(time);
            _rundelay.Add(delay);
            TotalNum++;

        }

        public void ModifyItem(Int32 index, String name, Int32 count, Int32 time, Int32 delay)
        {
            _methodname[index] = name;
            _runcount[index] = count;
            _runtime[index] = time;
            _rundelay[index] = delay;
        }

        public bool RemoveItem(String name, Int32 count, Int32 time, Int32 delay)
        {
            int indexno = 0;
            if (_methodname.Contains(name))
            {
                indexno &= _methodname.IndexOf(name);
                indexno &= _runcount.IndexOf(count);
                indexno &= _runtime.IndexOf(time);
                indexno &= _rundelay.IndexOf(delay);

                if (indexno == 0)
                {
                    _methodname.RemoveAt(indexno);
                    _runcount.RemoveAt(indexno);
                    _runtime.RemoveAt(indexno);
                    _rundelay.RemoveAt(indexno);
                    TotalNum--;
                    return true;
                }
            }
            return false;
        }

        private bool SaveIni(IniFile saveini)
        {
            int i;
            int count = _methodname.Count & _runcount.Count & _runtime.Count;
            for (i = 0; i < _methodname.Count; i++)
            {
                saveini.WriteString("运行表", i.ToString(), _methodname[i]);
            }

            for (i = 0; i < _runcount.Count; i++)
            {
                saveini.WriteInteger("循环", i.ToString(), _runcount[i]);
            }

            for (i = 0; i < _runtime.Count; i++)
            {
                saveini.WriteInteger("运行时间", i.ToString(), _runtime[i]);
            }

            for (i = 0; i < _rundelay.Count; i++)
            {
                saveini.WriteInteger("间隔时间", i.ToString(), _rundelay[i]);
            }

            if (TotalNum == count)
            {
                saveini.WriteInteger("Total", "count", count);
            }
            else
            {
                Log.Info("Run Table File Bug");
            }
            saveini.WriteString("Bat", "name", batname);
            return true;
        }

        public bool SaveRunTable()
        {
            return SaveIni(RunTableFile);
        }


        public bool SaveAsRunTable(String folder, String filename)
        {
            _asfilename = folder + "\\" + filename;
            try
            {
                if (!File.Exists(_asfilename))
                {
                    StreamWriter sw = new StreamWriter(_asfilename, false, Encoding.Unicode);
                    sw.WriteLine("[Bat]");
                    sw.WriteLine("[Total]");
                    sw.WriteLine("[运行表]");
                    sw.WriteLine("[循环]");
                    sw.WriteLine("[运行时间]");
                    sw.WriteLine("[间隔时间]");
                    sw.Close();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }

            AsRunTableFile = new IniFile(_asfilename);

            return SaveIni(AsRunTableFile);
        }




    }
}
