using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
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 HighWayIot.Winform.UserControlPages
{
    public partial class AlarmConfigPage : UserControl
    {
        private static SysErrorLogService sysErrorLogService = SysErrorLogService.Instance;

        private List<SysErrorLogEntity> Lists;

        public AlarmConfigPage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            LogDataGridView.AutoGenerateColumns = false;

            Lists = sysErrorLogService.GetErrorLogInfos();

            LogDataGridView.DataSource = null;
            LogDataGridView.DataSource = Lists;
        }

        private void SelectErrorLog_Click(object sender, EventArgs e)
        {
            List<SysErrorLogEntity> list = sysErrorLogService.GetErrorLogInfos();

            int? p1 = GeneralUtils.StringNullOrToInt(P1TextBox.Text);
            int? p2 = GeneralUtils.StringNullOrToInt(P2TextBox.Text);
            int? p3 = GeneralUtils.StringNullOrToInt(P3TextBox.Text);

            string logText = LogTextTextBox.Text.Trim();
            string operatorName = OperatorNameTextBox.Text.Trim();
            bool logTimeChecked = IsCheckByLogTime.Checked;
            DateTime logBeginTime = SelectLogBeginTime.Value;
            DateTime logEndTime = SelectLogEndTime.Value;

            Lists = list.Where(x =>
                (string.IsNullOrEmpty(logText) || x.Text.Contains(logText)) &&
                (string.IsNullOrEmpty(operatorName) || x.Operator == operatorName) &&
                (!p1.HasValue || x.P1 == p1.Value) &&
                (!p2.HasValue || x.P2 == p2.Value) &&
                (!p3.HasValue || x.P3 == p3.Value) &&
                (!logTimeChecked || (x.LogTime >= logBeginTime && x.LogTime <= logEndTime))
            ).ToList();

            LogDataGridView.DataSource = null;
            LogDataGridView.DataSource = Lists;
        }
    }
}
