using HighWayIot.Repository.domain;
using HighWayIot.TouchSocket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TouchSocket.Sockets;

namespace RFIDSocket
{
    public partial class RFIDSocket : Form
    {
        private static TcpServer Server = TcpServer.Instance;

        private static DataAnalysis RFIDData = DataAnalysis.Instance;


        string Port = "1234";
        string IP = "127.0.0.1";

        int PageNo = 1;

        public RFIDSocket()
        {
            InitializeComponent();
            InitAction();
        }

        private void InitAction()
        {
            if (Server.State != ServerState.Running)
            {
                MonitorState.Text = "关";
                MonitorState.BackColor = Color.Yellow;
            }
            else if (Server.State == ServerState.Running)
            {
                MonitorState.Text = "开";
                MonitorState.BackColor = Color.LightGreen;
            }

            Port = PortText.Text;
            IP = IPText.Text;

            
        }

        private void MonitorOnOff_Click(object sender, EventArgs e)
        {
            if (Server.State != ServerState.Running)
            {
                if (Server.ServerStart(IP, Port))
                {
                    MessageBox.Show("监听服务启动成功！");
                    TableTimer.Enabled = true;
                }
                else
                {
                    MessageBox.Show("监听服务启动失败！");
                }
            }
            else if (Server.State == ServerState.Running)
            {
                if (Server.ServerStop())
                {
                    MessageBox.Show("监听服务关闭成功！");
                    TableTimer.Enabled = false;
                }
                else
                {
                    MessageBox.Show("监听服务关闭失败！");
                }
            }
        }

        private void SetPort_Click(object sender, EventArgs e)
        {
            Port = PortText.Text;
            IP = IPText.Text;
        }

        private void TableTimer_Tick(object sender, EventArgs e)
        {
            if (Server.State != ServerState.Running)
            {
                MonitorOnOff.Text = "启动监听";
                MonitorState.Text = "关";
                MonitorState.BackColor = Color.Yellow;
            }
            else if (Server.State == ServerState.Running)
            {
                MonitorOnOff.Text = "关闭监听";
                MonitorState.Text = "开";
                MonitorState.BackColor = Color.LightGreen;
            }

            RFIDData.GetData();

            CotentData.DataSource = null;
            StateData.DataSource = null;
            HeartbeatData.DataSource = null;

            switch (PageNo)
            {
                case 1:  ContentPages(0); PageRange.Text = "0 - 50"; break;
                case 2:  ContentPages(50); PageRange.Text = "51 - 100"; break;
                case 3: ContentPages(100); PageRange.Text = "101 - 150"; break;
                case 4: ContentPages(150); PageRange.Text = "151 - 200"; break;
                    default: ContentPages(0); PageRange.Text = "0 - 50"; break;
            }

            StateData.DataSource = RFIDData.AlarmState;
            HeartbeatData.DataSource = RFIDData.HeartbeatsState;
        }

        private void RFIDSocket_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Server.State == ServerState.Running)
            {
                if (!Server.ServerStop())
                {
                    MessageBox.Show("监听服务关闭失败！ 请成功关闭后再退出");
                    e.Cancel = true;
                }
                if (!Server.ServerDispose())
                {
                    MessageBox.Show("监听服务释放失败！ 请再次尝试退出");
                    e.Cancel = true;
                }
            }
        }

        private void ContentPages(int skip)
        {
            CotentData.DataSource = RFIDData.rFIDContents.Skip(skip).Take(50).ToList();
        }

        private void PgUp_Click(object sender, EventArgs e)
        {
            if(PageNo == 1)
            {
                MessageBox.Show("已经是首页！"); 
                return;
            }
            PageNo--;
        }

        private void PgDn_Click(object sender, EventArgs e)
        {
            if (PageNo == 4)
            {
                MessageBox.Show("已经是尾页！");
                return;
            }
            PageNo++;
        }

        private void LogStart_Click(object sender, EventArgs e)
        {
            RFIDLog rFIDLog = new RFIDLog();
            rFIDLog.Show();
        }
    }
}
