using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace UniversalEditor
{
    public partial class DataGrid : DevExpress.XtraEditors.XtraForm
    {
        frmMain mainfrm;
        List<UpdatedCell> list = new List<UpdatedCell>();

        public DataGrid(frmMain main)
        {
            InitializeComponent();
            mainfrm = main;

            
        }

        public class UpdatedCell
        {
            // 'UC means UpdatedCll
            public int UCFocusedRow;
            public int UCFocusedColumnIndex;
            public string UCFieldName;

            public void New()
            {
                UCFocusedRow = -1;
                UCFocusedColumnIndex = -1;
                UCFieldName = String.Empty;
            }
        }

        private void View_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            string t = mainfrm.FileTabs.SelectedTabPage.Text;
            string n = t.Replace("*", String.Empty);
            mainfrm.FileTabs.SelectedTabPage.Text = n;
            mainfrm.FileTabs.SelectedTabPage.Text = mainfrm.FileTabs.SelectedTabPage.Text + "*";
            
            UpdatedCell currCell = new UpdatedCell();
            currCell.UCFocusedRow = e.RowHandle;
            currCell.UCFocusedColumnIndex = e.Column.AbsoluteIndex;
            currCell.UCFieldName = e.Column.FieldName;

            list.Add(currCell);
        }

        private void View_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                View.DeleteSelectedRows();
                string t = mainfrm.FileTabs.SelectedTabPage.Text;
                string n = t.Replace("*", String.Empty);
                mainfrm.FileTabs.SelectedTabPage.Text = n;
                mainfrm.FileTabs.SelectedTabPage.Text = mainfrm.FileTabs.SelectedTabPage.Text + "*";
            }
            if (e.KeyData == (Keys.Control | Keys.N | Keys.R))
            {
                ((DevExpress.XtraGrid.Views.Grid.GridView)mainfrm.dataGrid.Views[0]).AddNewRow();
                string t = mainfrm.FileTabs.SelectedTabPage.Text;
                string n = t.Replace("*", String.Empty);
                mainfrm.FileTabs.SelectedTabPage.Text = n;
                mainfrm.FileTabs.SelectedTabPage.Text = mainfrm.FileTabs.SelectedTabPage.Text + "*";
            }
            if (e.KeyData == (Keys.Control | Keys.S))
            {
                if (mainfrm.FileTabs.TabPages.Count < 1) return;
                mainfrm.SaveTo(mainfrm.file.Path);
            }
            if (e.KeyData == (Keys.Control | Keys.C))
            {
                if (mainfrm.FileTabs.TabPages.Count < 1) return;
                mainfrm.RCopy.PerformClick();
            }
            if (e.KeyData == (Keys.Control | Keys.V))
            {
                if (mainfrm.FileTabs.TabPages.Count < 1) return;
                View.ClearSelection();
                mainfrm.RPaste.PerformClick();
            }
        }

        private void View_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            Color prevColor = e.Appearance.BackColor;

            string HighlightHexValue = "#00A9FE";
            Color Highlight = System.Drawing.ColorTranslator.FromHtml(HighlightHexValue);

            foreach(UpdatedCell c in list)
            {
                if (e.RowHandle == c.UCFocusedRow && e.Column.AbsoluteIndex == c.UCFocusedColumnIndex && e.Column.FieldName == c.UCFieldName)
                {
                    e.Appearance.BackColor = Highlight;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (e.Appearance.BackColor != Highlight)
                {
                    e.Appearance.BackColor = prevColor;
                }
            }
        }

        private void DataGrid_Load(object sender, EventArgs e)
        {
            //gridView1.Columns["Trademark"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            if (View.Columns["ID"] != null)
            {
                View.Columns["ID"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            }
        }

        private void View_InitNewRow(object sender, InitNewRowEventArgs e)
        {

        }
    }
}