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 Aram_CRM_Project.UIL
{
    public partial class frmConnection : Form
    {
        public frmConnection()
        {
            InitializeComponent();
        }

        private void frmConnection_Load(object sender, EventArgs e)
        {
            try
            {
                Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
                string TextFile;
                string[] NetworkConnection;
                string[] Ip;
                string Password;

                System.IO.StreamReader Reader = new System.IO.StreamReader(Application.StartupPath + @"\NC.dat");
                TextFile = Reader.ReadToEnd();
                Reader.Close();

                if (TextFile != "")
                {
                    NetworkConnection = TextFile.Split('&');
                    if (NetworkConnection[0] == "Server")
                    {
                        rdoServer.Checked = true;
                        txtIp1.Enabled = false;
                        txtIp2.Enabled = false;
                        txtIp3.Enabled = false;
                        txtIp4.Enabled = false;
                    }
                    else if (NetworkConnection[0] == "Client")
                    {
                        rdoClient.Checked = true;
                        Ip = NetworkConnection[1].Split('.');
                        txtIp1.Text = Ip[0];
                        txtIp2.Text = Ip[1];
                        txtIp3.Text = Ip[2];
                        txtIp4.Text = Ip[3];
                    }


                    Password = utility.Decode(NetworkConnection[2]);
                    txtPass.Text = Password;
                    txtConfirmPass.Text = Password;

                }
                else
                {
                    rdoServer.Checked = true;
                }

                Reader.Close();
            }
            catch(Exception exp)
            {
            }
        }

        private void rdoServer_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoServer.Checked == true)
            {
                txtIp1.Enabled = false;
                txtIp2.Enabled = false;
                txtIp3.Enabled = false;
                txtIp4.Enabled = false;
                txtPass.Enabled = false;
                txtConfirmPass.Enabled = false;
            }
            else
            {
                txtIp1.Enabled = true;
                txtIp2.Enabled = true;
                txtIp3.Enabled = true;
                txtIp4.Enabled = true;
                txtPass.Enabled = true;
                txtConfirmPass.Enabled = true;
            }
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            try
            {
                Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
                string SystemType;
                string Ip;
                string Password;
                bool ErrorFlag = false;

                errConnection.Clear();
                if (rdoClient.Checked != true && rdoServer.Checked != true)
                {
                    errConnection.SetIconAlignment(this.rdoServer, ErrorIconAlignment.MiddleLeft);
                    errConnection.SetError(this.rdoServer, Properties.Resources.ResourceManager.GetString("ErrSelectNetwork"));
                    ErrorFlag = true;
                }
                if (rdoClient.Checked == true && (txtIp1.Text.Trim() == "" || txtIp2.Text.Trim() == "" || txtIp3.Text.Trim() == "" || txtIp4.Text.Trim() == ""))
                {
                    errConnection.SetIconAlignment(this.txtIp1, ErrorIconAlignment.MiddleLeft);
                    errConnection.SetError(this.txtIp1, Properties.Resources.ResourceManager.GetString("ErrEmptyIp"));
                    ErrorFlag = true;
                }
                if (rdoClient.Checked == true && txtPass.Text.Trim() == "")
                {
                    errConnection.SetIconAlignment(this.txtPass, ErrorIconAlignment.MiddleLeft);
                    errConnection.SetError(this.txtPass, Properties.Resources.ResourceManager.GetString("ErrEmptyPassword"));
                    ErrorFlag = true;
                }
                if (rdoClient.Checked == true && (txtPass.Text.Trim() != txtConfirmPass.Text.Trim()))
                {
                    errConnection.SetIconAlignment(this.txtPass, ErrorIconAlignment.MiddleLeft);
                    errConnection.SetError(this.txtPass, Properties.Resources.ResourceManager.GetString("ErrNotEqualPassword"));
                    ErrorFlag = true;
                }
                if (ErrorFlag)
                {
                    return;
                }

                if (rdoServer.Checked == true)
                {
                    SystemType = "Server";
                    Ip = ".";
                }
                else
                {
                    SystemType = "Client";
                    Ip = txtIp1.Text.Trim() + "." + txtIp2.Text.Trim() + "." + txtIp3.Text.Trim() + "." + txtIp4.Text.Trim();
                }

                Password = utility.Encode(txtPass.Text.Trim());

                //Remove Content Of File
                System.IO.StreamWriter Remover = new System.IO.StreamWriter(Application.StartupPath + @"\NC.dat");
                Remover.Write("");
                Remover.Close();
                //Write File
                System.IO.StreamWriter Writer = System.IO.File.AppendText(Application.StartupPath + @"\NC.dat");
                Writer.Write("");
                Writer.WriteLine(SystemType + "&" + Ip + "&" + Password);
                Writer.Close();
                
                this.Dispose();
                
                
                
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        
    }
}
