using ManualSoftwareSystem.lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ManualSoftwareSystem.Operation_Manual
{
    public partial class FrmOperationInputInformation : Form
    {
        private string csvPartFile = string.Empty;
        private List<List<string>> Data = new List<List<string>>();
        private string csvPartFileOperationInputInformation = System.IO.Directory.GetCurrentDirectory() + "\\OperationInputInformation.sys";
        private string ErrorPart = System.IO.Directory.GetCurrentDirectory() + "\\Error";
        public dynamic objLanguage;


        public FrmOperationInputInformation(string pathFile)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmOperationInputInformation_Load(object sender, EventArgs e)
        {
            string strInputInformation = "Input information";
            string strOK = "OK";
            string strCancel = "Cancel";
            //string strDetailedcomment = "Detailed comment";
            string strDetailedcomment = "Error details";
            string strAddDetailedComment = "Add";
            string strDelDetailedComment = "Del";
            //string strActionContent = "Action content";
            string strActionContent = "Action details";
            string strAddActionContent = "Add";
            string strDeActionContent = "Del";

            lblInputInformation.Text = objLanguage.Translate(strInputInformation);
            btnOK.Text = objLanguage.Translate(strOK);
            btnCancel.Text = objLanguage.Translate(strCancel);
            lblDetailedcomment.Text = objLanguage.Translate(strDetailedcomment);
            btnAddDetailedComment.Text = objLanguage.Translate(strAddDetailedComment);
            btnDelDetailedComment.Text = objLanguage.Translate(strDelDetailedComment);
            lblActionContent.Text = objLanguage.Translate(strActionContent);
            btnAddActionContent.Text = objLanguage.Translate(strAddActionContent);
            btnDeActionContent.Text = objLanguage.Translate(strDeActionContent);

            if (!File.Exists(csvPartFileOperationInputInformation))
            {
                using (StreamWriter sw = File.CreateText(csvPartFileOperationInputInformation))
                {
                    sw.WriteLine("");
                    sw.WriteLine("");
                }
            }

            FrmOperationManual.intSSstartError = 0;

            timer1.Enabled = true;


            FrmOperationManualOperator.ErrorCloseShare = false;

            cmbDetailedComment.Items.Clear();
            cmbActionContent.Items.Clear();

            List<string> tmp = null;
            Data = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFileOperationInputInformation))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Data.Add(tmp);

                    if (i == 0)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[0][j] != string.Empty)
                            {
                                cmbDetailedComment.Items.Add(this.Data[0][j]);
                            }

                        }
                    }

                    if (i == 1)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[1][j] != string.Empty)
                            {
                                cmbActionContent.Items.Add(this.Data[1][j]);
                            }

                        }
                    }


                    i = i + 1;
                }

            }//using end

        }

        private void btnAddDetailedComment_Click(object sender, EventArgs e)
        {
            if (cmbDetailedComment.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, "Detailed Comment is Blanks",
                                       "Warning", MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbDetailedComment.FindStringExact(cmbDetailedComment.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbDetailedComment.Items.Add(cmbDetailedComment.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbDetailedComment.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbDetailedComment.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbDetailedComment.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInputInformation, 1);

            }
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            string[] arrLine = File.ReadAllLines(fileName);
            arrLine[line_to_edit - 1] = newText;
            File.WriteAllLines(fileName, arrLine);
        }

        private void btnDelDetailedComment_Click(object sender, EventArgs e)
        {
            if (cmbDetailedComment.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, "Detailed Comment is Blanks",
                                       "Warning", MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbDetailedComment.FindStringExact(cmbDetailedComment.Text.Trim());

            if (index > -1)
            {
                cmbDetailedComment.Items.RemoveAt(index);

                if (cmbDetailedComment.Items.Count == 0)
                {
                    cmbDetailedComment.Items.Clear();
                    cmbDetailedComment.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbDetailedComment.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbDetailedComment.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbDetailedComment.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInputInformation, 1);
            }
        }

        private void btnAddActionContent_Click(object sender, EventArgs e)
        {
            if (cmbActionContent.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, "Action Content is Blanks",
                                       "Warning", MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbActionContent.FindStringExact(cmbActionContent.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbActionContent.Items.Add(cmbActionContent.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbActionContent.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbActionContent.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbActionContent.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInputInformation, 2);

            }
        }

        private void btnDeActionContent_Click(object sender, EventArgs e)
        {
            if (cmbActionContent.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, "Action Content is Blanks",
                                       "Warning", MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbActionContent.FindStringExact(cmbActionContent.Text.Trim());

            if (index > -1)
            {
                cmbActionContent.Items.RemoveAt(index);

                if (cmbActionContent.Items.Count == 0)
                {
                    cmbActionContent.Items.Clear();
                    cmbActionContent.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbActionContent.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbActionContent.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbActionContent.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInputInformation, 2);
            }
        }

        private void btnOrderAtoZDetailedComment_Click(object sender, EventArgs e)
        {
            cmbDetailedComment.Sorted = true;
        }

        private void btnOrderZtoADetailedComment_Click(object sender, EventArgs e)
        {
            cmbDetailedComment.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbDetailedComment.Items.Count; i++)
            {

                TempList.Add(cmbDetailedComment.Items[i].ToString());

            }

            List<string> sortedDetailedComment = TempList.OrderByDescending(DetailedComment => DetailedComment).ToList();
            cmbDetailedComment.Items.Clear();
            foreach (string DetailedCommentName in sortedDetailedComment)
                cmbDetailedComment.Items.Add(DetailedCommentName);

        }

        private void btnOrderAtoZActionContent_Click(object sender, EventArgs e)
        {
            cmbActionContent.Sorted = true;
        }

        private void btnOrderZtoAActionContent_Click(object sender, EventArgs e)
        {
            cmbActionContent.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbActionContent.Items.Count; i++)
            {

                TempList.Add(cmbActionContent.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbActionContent.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbActionContent.Items.Add(ActionContentName);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(ErrorPart) == false)
            {
                Directory.CreateDirectory(ErrorPart);
            }

            string fileName = csvPartFile;
            //string pathName = Path.GetDirectoryName(fileName);
            string result;
            string pathFileNameError = string.Empty;

            result = Path.GetFileName(fileName);

            result = result.Substring(0, result.Length - 1) + "2";

            pathFileNameError = ErrorPart + "\\" + result;

            using (FileStream fs = File.Create(pathFileNameError)) ;

            //CSV Encoding
            System.Text.Encoding enc =
                System.Text.Encoding.UTF8;

            System.IO.StreamWriter sr =
                new System.IO.StreamWriter(pathFileNameError, false, enc);
            sr.WriteLine(FrmOperationManualOperator.strFileMaster + ","
                + FrmOperationManualOperator.strPageError + ","
                + FrmOperationManualOperator.strLoginError + ","
                + DateTime.Now.ToString("yyyy-MM-dd") + ","
                + DateTime.Now.ToString("HH:mm:ss") + ","
                + cmbDetailedComment.Text.Trim().Replace(",", "") + ","
                + cmbActionContent.Text.Trim().Replace(",", "") + ","
                + FrmOperationManualOperator.WeatherShare + ","
                + FrmOperationManualOperator.TemperatureShare + ","
                + FrmOperationManualOperator.HumidityShare + ","
                + FrmOperationManualOperator.RemarksShare
                );
            sr.Close();

            FrmOperationManualOperator.ErrorCloseShare = true;

            FrmOperationManualOperator.strFileMaster = string.Empty;
            FrmOperationManualOperator.strPageError = string.Empty;
            FrmOperationManualOperator.strLoginError = string.Empty;

            timer1.Enabled = false;

            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FrmOperationManualOperator.ErrorCloseShare = true;
            this.Close();
        }

        private void cmbDetailedComment_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbActionContent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbDetailedComment_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy == false)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            FrmOperationManual.intSSstartError = FrmOperationManual.intSSstartError + 1;

            if (FrmOperationManual.intSSstartError > 59)
            {
                FrmOperationManual.intSSstartError = 0;
                FrmOperationManual.intMMstartError = FrmOperationManual.intMMstartError + 1;
            }

            if (FrmOperationManual.intMMstartError > 59)
            {
                FrmOperationManual.intMMstartError = 0;
                FrmOperationManual.intSSstartError = FrmOperationManual.intSSstartError + 1;
            }

            if (FrmOperationManual.intSSstartError.ToString().Length == 1)
            {
                FrmOperationManual.strSSstartError = "0" + FrmOperationManual.intSSstartError.ToString();
            }
            else
            {
                FrmOperationManual.strSSstartError = FrmOperationManual.intSSstartError.ToString();
            }

            if (FrmOperationManual.intMMstartError.ToString().Length == 1)
            {
                FrmOperationManual.strMMstartError = "0" + FrmOperationManual.intMMstartError.ToString();
            }
            else
            {
                FrmOperationManual.strMMstartError = FrmOperationManual.intMMstartError.ToString();
            }

            if (FrmOperationManual.intHHstartError.ToString().Length == 1)
            {
                FrmOperationManual.strHHstartError = "0" + FrmOperationManual.intHHstartError.ToString();
            }
            else
            {
                FrmOperationManual.strHHstartError = FrmOperationManual.intHHstartError.ToString();
            }
        }
    }
}
