using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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 FrmOperationConfirmation : Form
    {
        private string csvPartFile = string.Empty;
        private Int32 rowCount = 0;
        private string loginUser = string.Empty;
        public dynamic objLanguage;

        public FrmOperationConfirmation(DataGridView dgView1, string pathFile, string loginUser)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.loginUser = loginUser;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
            rowCount = dgView1.SelectedRows.Count;
            DataObject dataObj = dgView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);

        }

        private void FrmOperationConfirmation_Load(object sender, EventArgs e)
        {
            string strConfirmation = "Confirmation";
            string strRestart = "Restart";
            string strHandingOver = "Handing\r\nOver";
            string strEND = "END";

            lblConfirmation.Text = objLanguage.Translate(strConfirmation);
            btnRestart.Text = objLanguage.Translate(strRestart);
            btnHandingOver.Text = objLanguage.Translate(strHandingOver);
            btnEND.Text = objLanguage.Translate(strEND);

            FrmOperationManualOperator.HandingOverShare = false;

            PasteClipboardValue();
        }

        private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)
        {
            Dictionary<int, Dictionary<int, string>>
            copyValues = new Dictionary<int, Dictionary<int, string>>();

            String[] lines = clipboardValue.Split('\n');

            for (int i = 0; i <= lines.Length - 1; i++)
            {
                copyValues[i] = new Dictionary<int, string>();
                String[] lineContent = lines[i].Split('\t');

                //if an empty cell value copied, then set the dictionary with an empty string
                //else Set value to dictionary
                if (lineContent.Length == 0)
                    copyValues[i][0] = string.Empty;
                else
                {
                    for (int j = 0; j <= lineContent.Length - 1; j++)
                        copyValues[i][j] = lineContent[j];
                }
            }
            return copyValues;
        }

        private void PasteClipboardValue()
        {

            for (int i = 0; i < rowCount; i++)
            {

                dataGridView2.Rows.Add();
            }

            //Get the starting Cell
            //DataGridViewCell startCell = GetStartCell(dataGridView2);
            //Get the clipboard value in a dictionary
            Dictionary<int, Dictionary<int, string>> cbValue =
                    ClipBoardValues(Clipboard.GetText());

            //dataGridView2.Rows.Clear();

            //int rowCheck = -1;
            bool rowCheck = false;
            int iRowIndex = 0; // startCell.RowIndex;
            foreach (int rowKey in cbValue.Keys)
            {
                //rowCheck = false;
                //foreach (var item in rowsNo)
                //{
                //    if (rowKey == item)
                //    {
                //        rowCheck = true;
                //        break;
                //    }
                //}

                //if (rowCheck == true)
                //{
                //    continue;
                //}

                //dataGridView2.Rows.Add();

                int iColIndex = 0; // startCell.ColumnIndex;
                foreach (int cellKey in cbValue[rowKey].Keys)
                {
                    if (cellKey == 0)
                    {
                        continue;
                    }

                    //Check if the index is within the limit
                    if (iColIndex <= dataGridView2.Columns.Count - 1
                    && iRowIndex <= dataGridView2.Rows.Count - 1)
                    {
                        DataGridViewCell cell = dataGridView2[iColIndex, iRowIndex];

                        //Copy to selected cells if 'chkPasteToSelectedCells' is checked
                        //if ((chkPasteToSelectedCells.Checked && cell.Selected) ||
                        //    (!chkPasteToSelectedCells.Checked))

                        cell.Value = cbValue[rowKey][cellKey];



                    }
                    iColIndex++;
                }
                iRowIndex++;
            }
        }

        private void btnRestart_Click(object sender, EventArgs e)
        {
            //dataGridView2.SelectAll();

            //FrmOperationManual frmOperationManual = new FrmOperationManual(true, dataGridView2, csvPartFile, "", loginUser);

            //this.Dispose();

            //frmOperationManual.ShowDialog();

            FrmOperationManual.RestartClick = true;

            FrmOperationManualOperator.SerialNumberShare = string.Empty;

            this.Close();
        }

        private void btnHandingOver_Click(object sender, EventArgs e)
        {

            FrmOperationManualOperator frmOperationManualOperator = new FrmOperationManualOperator(csvPartFile, "");

            FrmOperationManualOperator.HandingOverShare = true;

            FrmOperationManualOperator.SerialNumberShare = string.Empty;

            this.Dispose();

            frmOperationManualOperator.ShowDialog();

        }

        private void btnEND_Click(object sender, EventArgs e)
        {
            FrmOperationManual.ENDClick = true;

            this.Close();
        }

        private void btnERR_Click(object sender, EventArgs e)
        {
            FrmOperationInputInformation frmOperationInputInformation = new FrmOperationInputInformation(csvPartFile);

            FrmOperationManualOperator.ErrorClickShare = true;

            this.Dispose();

            frmOperationInputInformation.ShowDialog();
        }
    }
}
