using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages
{
    public partial class page_RichEdit : FormBase
    {
        public page_RichEdit()
        {
            InitializeComponent();
            LoadDocument();
            richEditControl1.Options.HorizontalRuler.Visibility = RichEditRulerVisibility.Hidden;
            richEditControl1.Options.VerticalRuler.Visibility = RichEditRulerVisibility.Hidden;
        }

        public page_RichEdit(DataRow Datas)
        {
            InitializeComponent();
            ItemData = Datas;
            LoadDocument();
            richEditControl1.Options.HorizontalRuler.Visibility = RichEditRulerVisibility.Hidden;
            richEditControl1.Options.VerticalRuler.Visibility = RichEditRulerVisibility.Hidden;

        }

        DataRow ItemData { get; set; }

        WordTabelPoints Rangs { get; set; }

        TableCell SelectCell { get; set; }

        TableCell OtherExceptionCell { get; set; }

        public void LoadDocument()
        {
            string path = @"E:\locals\设备维护管理软件\Documentation\WI-013-R37 DMU50 DMU60加工中心定期维护点检表.docx";
            if (File.Exists(path))
                richEditControl1.LoadDocument(path, DocumentFormat.Doc);
            richEditControl1.ReadOnly = true;

            Rangs = new WordTabelPoints();

            int StartRowNumber = -1;

            richEditControl1.Document.Tables.First.ForEachRow((TableRow row, int rowIndex) =>
            {
                string StartValue = richEditControl1.Document.GetText(row.Cells.First.Range)?.Trim();
                if (Rangs.RowIndex > 0 && Rangs.RowCount == 0 && row.Cells.First.ColumnSpan > 1)
                {
                    Rangs.RowCount = row.Previous.Index - StartRowNumber;
                }

                string EndValue = richEditControl1.Document.GetText(row.Cells.Last.Range)?.Trim();
                if (EndValue == "十二月")
                {
                    Rangs.CellIndex = row.Cells.Last.Index - 11;
                    Rangs.RowIndex = row.Next.Index;
                    StartRowNumber = rowIndex;
                }

                if (StartValue == "其它异常处理")
                {
                    OtherExceptionCell = row.FirstCell.Next;
                }                
            });
        }

        private void richEditControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!richEditControl1.IsSelectionInTable())
                return;

            RichEditControl richEdit = sender as RichEditControl;
            DocumentPosition start = richEdit.Document.Selection.Start;

            Table selectedTable = GetSelectedTable(richEdit.Document, start);
            if (selectedTable != null)
            {
                TableCell selectedCell = GetSelectedTableCell(selectedTable, start);
                if (selectedCell != null)
                {
                    int rowIndex = selectedCell.Row.Index;
                    int columnIndex = selectedCell.Index;
                    if (IsCoordinateInRange(Rangs, rowIndex, columnIndex))
                    {
                        SelectCell = richEditControl1.Document.Tables.First[rowIndex, columnIndex];
                        popupMenu1.ShowPopup(richEditControl1.PointToScreen(e.Location));
                    }
                }
            }
        }

        private Table GetSelectedTable(Document document, DocumentPosition position)
        {
            foreach (Table table in document.Tables)
            {
                if (table.Range.Contains(position))
                {
                    return table;
                }
            }
            return null;
        }

        private TableCell GetSelectedTableCell(Table table, DocumentPosition position)
        {
            TableCell tc = null;
            table.ForEachCell((TableCell cell, int rowIndex, int cellIndex) =>
            {
                if (cell.Range.Contains(position))
                {
                    tc = cell;
                }
            });

            return tc;
        }


        private void btn_Ok_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SelectCell != null)
            {
                richEditControl1.Document.Replace(SelectCell.ContentRange, "√");
            }
        }

        private void btn_NA_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SelectCell != null && OtherExceptionCell != null)
            {
                // Show the input box
                string result = XtraInputBox.Show("其它异常处理:", "输入", "");

                if (!string.IsNullOrEmpty(result))
                {
                    string contentString = richEditControl1.Document.GetText(OtherExceptionCell.Range)?.Trim();
                    contentString += Environment.NewLine + result;

                    richEditControl1.Document.Replace(OtherExceptionCell.ContentRange, contentString);
                    richEditControl1.Document.Replace(SelectCell.ContentRange, "N/A");
                }


            }
        }

        private void btn_Triangle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SelectCell != null)
            {
                richEditControl1.Document.Replace(SelectCell.ContentRange, "△");
            }
        }

        private void btn_None_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SelectCell != null)
            {
                richEditControl1.Document.Replace(SelectCell.ContentRange, "/");
            }
        }
    }
}
