using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManualSoftwareSystem
{

    public partial class FrmInspectionWork : Form
    {
        private bool viewer = false;
        private List<List<string>> Master = new List<List<string>>();
        private Int32 rowCount = 0;
        private List<Int32> rowsNo = new List<Int32>();
        private Int32 intRowSelect = 0;
        private Int32 intColPicSelect = 0;

        public FrmInspectionWork(bool viewer, DataGridView dgView1)
        {
            InitializeComponent();
            this.viewer = viewer;
            rowCount = dgView1.SelectedRows.Count;
            DataObject dataObj = dgView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

        private void FrmInspectionWork_Load(object sender, EventArgs e)
        {
            if (viewer == true)
            {
                if (rowCount == 0)
                {
                    MessageBox.Show("Please select a row", "Viewer",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Close();
                }


                ViewClipboardValue();
                PasteClipboardValue();
            }

            //if (dataGridView2.Rows.Count == 0)
            //{
            //    MessageBox.Show("Please check Manual Column", "Viewer",
            //    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    Close();
            //}

            //lblPage.Text = "1/" + dataGridView2.Rows.Count.ToString();

            btnBackStart.Enabled = false;
            btnBack.Enabled = false;

            ShowData(0, 0);

            intRowSelect = 0;

            intColPicSelect = 0;

            timer1.Enabled = true;
        }

        private void ShowData(Int32 intRow, Int32 intColPic)
        {
            lblPage.Text = (intRow + 1).ToString() + "/" + dataGridView2.Rows.Count.ToString();

            if (dataGridView2[4, intRow].Value != null)
            {
                lblTitle.Text = dataGridView2[4, intRow].Value.ToString();
            }
            else
            {
                lblTitle.Text = string.Empty;
            }

            if (dataGridView2[5, intRow].Value != null)
            {
                lblMessage.Text = dataGridView2[5, intRow].Value.ToString();
            }
            else
            {
                lblMessage.Text = string.Empty;
            }

            if (dataGridView2[3, intRow].Value != null)
            {
                txtComment.Text = dataGridView2[3, intRow].Value.ToString();
            }
            else
            {
                txtComment.Text = string.Empty;
            }

            if (dataGridView2[2, intRow].Value != null)
            {
                if (dataGridView2[2, intRow].Value != string.Empty)
                {
                    picMain3.Image = null;
                    byte[] data;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        data = Convert.FromBase64String(dataGridView2[2, intRow].Value.ToString());
                        picMain3.Image = byteArrayToImage((byte[])data);

                    }

                }
            }

            if (dataGridView2[intColPic, intRow].Value != null)
            {
                if (dataGridView2[intColPic, intRow].Value != string.Empty)
                {
                    picMain1_2.Image = null;
                    byte[] data;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        data = Convert.FromBase64String(dataGridView2[intColPic, intRow].Value.ToString());
                        picMain1_2.Image = byteArrayToImage((byte[])data);

                    }

                }
            }
        }

        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            System.Drawing.Bitmap bitmap = null;
            ImageConverter converter = new ImageConverter();
            System.Drawing.Image img =
            (System.Drawing.Image)converter.ConvertFrom(byteArrayIn);
            bitmap = (System.Drawing.Bitmap)img;
            return bitmap;
        }

        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 DataGridViewCell GetStartCell(DataGridView dgView)
        {
            ////get the smallest row,column index
            if (dgView.SelectedCells.Count == 0)
                return null;

            //int rowIndex = dgView.Rows.Count - 1;
            //int colIndex = dgView.Columns.Count - 1;

            //foreach (DataGridViewCell dgvCell in dgView.SelectedCells)
            //{
            //    if (dgvCell.RowIndex < rowIndex)
            //        rowIndex = dgvCell.RowIndex;
            //    if (dgvCell.ColumnIndex < colIndex)
            //        colIndex = dgvCell.ColumnIndex;
            //}

            //return dgView[colIndex, rowIndex];

            return dgView[0, 0];

        }

        private void ViewClipboardValue()
        {
            Master = new List<List<string>>();

            //Get the clipboard value in a dictionary
            Dictionary<int, Dictionary<int, string>> cbValue =
                    ClipBoardValues(Clipboard.GetText());

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

                    if (cellKey == 7)
                    {
                        if (cbValue[rowKey][cellKey].ToString() == "False")
                        {
                            rowsNo.Add(rowKey);
                            break;
                        }
                    }

                }
            }
        }

        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 btnOKNext_Click(object sender, EventArgs e)
        {
            intRowSelect = intRowSelect + 1;

            if (intRowSelect < dataGridView2.Rows.Count)
            {
                intColPicSelect = 0;
                ShowData(intRowSelect, intColPicSelect);
                btnBack.Enabled = true;
                btnBackStart.Enabled = true;
            }
            else
            {
                intRowSelect = intRowSelect - 1;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            intRowSelect = intRowSelect - 1;

            if (intRowSelect == 0)
            {
                intColPicSelect = 0;
                ShowData(intRowSelect, intColPicSelect);
                btnBack.Enabled = false;
                btnBackStart.Enabled = false;
            }
            else if (intRowSelect > 0)
            {
                intColPicSelect = 0;
                ShowData(intRowSelect, intColPicSelect);
            }
        }

        private void btnBackStart_Click(object sender, EventArgs e)
        {
            intRowSelect = 0;
            intColPicSelect = 0;
            ShowData(intRowSelect, intColPicSelect);
            btnBack.Enabled = false;
            btnBackStart.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (intRowSelect < dataGridView2.Rows.Count)
            {
                if (intColPicSelect == 0)
                {
                    intColPicSelect = 1;
                    ShowData(intRowSelect, intColPicSelect);
                }
                else if (intColPicSelect == 1)
                {
                    intColPicSelect = 0;
                    ShowData(intRowSelect, intColPicSelect);
                }
            }
        }
    }
}
