using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HouseHeatingTerminal
{


    public partial class StatusForm : Form
    {
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

        public StatusForm()
        {
            InitializeComponent();
        }

        private int getXPosition()
        {
            var mainForm = Program.GetWindow<MainForm>();
            return mainForm.Left + mainForm.Width / 2 - this.Width / 2;
        }
        private int getYPosition()
        {
            var mainForm = Program.GetWindow<MainForm>();
            return mainForm.Top + mainForm.Height / 2 - this.Height / 2;
        }

        public StatusForm(ConnectionStatus status)
        {
            InitializeComponent();
            switch (status)
            {

                case ConnectionStatus.Connecting:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting;
                        statusLbl.Text = "Connecting to the terminal";
                        this.Text = "Connecting";
                        break;
                    }
                case ConnectionStatus.Connected:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting_successful;
                        statusLbl.Text = "Connection established";
                        this.Text = "Connecting";
                        break;
                    }
                case ConnectionStatus.Disconnecting:
                    {
                        statusPictureBox.Image = Properties.Resources.disconnecting;
                        statusLbl.Text = "Disconnecting from the terminal";
                        this.Text = "Disconnecting";
                        //MessageBox.Show("Not Connected", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.Disconnected:
                    {
                        statusPictureBox.Image = Properties.Resources.disconnecting_successful;
                        statusLbl.Text = "Disconnected";
                        this.Text = "Disconnecting";
                        //MessageBox.Show("Not Connected", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.ConErr:
                    {
                        statusPictureBox.Image = Properties.Resources.conerr;
                        statusLbl.Text = "IO Error";
                        this.Text = "Connecting";
                        MessageBox.Show("COM Port not found on this computer.\nConnect a COM Port and try again!", "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.Error:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting_err;
                        statusLbl.Text = "Terminal not connected or enabled";
                        this.Text = "Connecting";
                        MessageBox.Show("The terminal is not connected to this computer or is not enabled to communicate.\n\nPlease connect the terminal to this computer via a COM port or enable the terminal and try again!", "Terminal not connected or enabled", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }

                default:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting;
                        break;
                    }
            }
            
        }

        private void displayTimer_Tick(object sender, EventArgs e)
        {
            displayTimer.Stop();
            Program.FormInvoke(this, displayTimer_Tick,sender,e);
            this.Close();
            this.Dispose();
        }



        private void StatusForm_Load(object sender, EventArgs e)
        {
            this.Left = getXPosition();
            this.Top = getYPosition();
        }

        public void StatusForm_ChangeState(ConnectionStatus status)
        {
            Program.ControlInvoke<ConnectionStatus>(statusLbl, StatusForm_ChangeState, status);
            switch (status)
            {

                case ConnectionStatus.Connecting:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting;
                        statusLbl.Text = "Connecting to the terminal";
                        break;
                    }
                case ConnectionStatus.Connected:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting_successful;
                        statusLbl.Text = "Connection established";
                        break;
                    }
                case ConnectionStatus.Disconnecting:
                    {
                        statusPictureBox.Image = Properties.Resources.disconnecting;
                        statusLbl.Text = "Disconnecting from the terminal";
                        //MessageBox.Show("Not Connected", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.Disconnected:
                    {
                        statusPictureBox.Image = Properties.Resources.disconnecting_successful;
                        statusLbl.Text = "Disconnected";
                        //MessageBox.Show("Not Connected", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.ConErr:
                    {
                        statusPictureBox.Image = Properties.Resources.conerr;
                        statusLbl.Text = "IO Error";
                        MessageBox.Show("COM Port not found on this computer.\nConnect a COM Port and try again!", "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                case ConnectionStatus.Error:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting_err;
                        statusLbl.Text = "Terminal not connected or enabled";
                        MessageBox.Show("The terminal is not connected to this computer or is not enabled to communicate.\n\nPlease connect the terminal to this computer via a COM port or enable the terminal and try again!", "Terminal not connected or enabled", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }

                default:
                    {
                        statusPictureBox.Image = Properties.Resources.connecting;
                        break;
                    }
            }
        }
        public void StatusForm_Finish()
        {

            displayTimer.Interval = 2400;
            displayTimer.Start();

        }
    }
}
