using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PXP.Crawler.API.Data;
using PXP.Crawler.API.Model;

namespace PXP.Crawler.API.Designers.Controls
{
    public partial class ErrorsUserControl : UserControl
    {
        public ErrorsUserControl()
        {
            InitializeComponent();
        }

        public void ErrorsClear()
        {
            errorsDataSet.Clear();
        }

        public void ErrorsAdd(ProjectItem pItem, CompilerError cError)
        {
            ErrorsDataSet.ErrorsRow eRow = errorsDataSet.Errors.NewErrorsRow();
            eRow.Error = cError.ErrorText;
            eRow.Line = cError.Line - 1;
            eRow.ProjectItem = pItem;
            eRow.Script = pItem.name;
            eRow.Col = cError.Column;
            eRow.Number = cError.ErrorNumber;
            eRow.CompilerError = cError;
            errorsDataSet.Errors.AddErrorsRow(eRow);
        }

        private void ErrorsUserControl_Load(object sender, EventArgs e)
        {            
            if (File.Exists("errors_grid_layout.xml"))
                errGridView.RestoreLayoutFromXml("errors_grid_layout.xml");
        }

        private void ErrorsUserControl_ControlRemoved(object sender, ControlEventArgs e)
        {
            errGridView.SaveLayoutToXml("errors_grid_layout.xml");
        }

        public delegate void ErrorRowClickHandler(object sender, ErrorsDataSet.ErrorsRow eRow);

        public event ErrorRowClickHandler ErrorRowClick;

        private void OnErrorRowClick(ErrorsDataSet.ErrorsRow eRow)
        {
            if (ErrorRowClick != null)
            {
                ErrorRowClick(this, eRow);
            }
        }

        private void errGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks >= 2)
            {
                DataRowView eRowView = (DataRowView)errGridView.GetRow(e.RowHandle);
                ErrorsDataSet.ErrorsRow eRow = (ErrorsDataSet.ErrorsRow) eRowView.Row;
                OnErrorRowClick(eRow);
            }
        }
    }
}
