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;
using ManualSoftwareSystem.lib;
using ManualSoftwareSystem.Operation_Manual;

namespace ManualSoftwareSystem
{
    public partial class FrmSetting : Form
    {
        private List<List<string>> Settings = new List<List<string>>();
        private string csvPartFile = System.IO.Directory.GetCurrentDirectory() + "\\Settings.sys";
        public dynamic objLanguage;

        public FrmSetting()
        {
            InitializeComponent();
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {

            string strSetting = "Setting";
            string strSettingUserInformation = "Setting user Information";
            string strUserID = "User ID";
            string strPassword = "Password";
            string strMasterMaintenanceUserInformation = "Master Maintenance user Information";
            string strUserID_M = "User ID";
            string strPassword_M = "Password";
            string strSave = "Save";
            string strCancel = "Cancel";


            lblSetting.Text = objLanguage.Translate(strSetting);
            lblSettingUserInformation.Text = objLanguage.Translate(strSettingUserInformation);
            lblUserID.Text = objLanguage.Translate(strUserID) + " :";
            lblPassword.Text = objLanguage.Translate(strPassword) + " :";
            lblMasterMaintenanceUserInformation.Text = objLanguage.Translate(strMasterMaintenanceUserInformation);
            lblUserID_M.Text = objLanguage.Translate(strUserID_M) + " :";
            lblPassword_M.Text = objLanguage.Translate(strPassword_M) + " :";
            btnSave.Text = objLanguage.Translate(strSave);
            btnCancel.Text = objLanguage.Translate(strCancel);


            List<string> tmp = null;
            Settings = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader(csvPartFile))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Settings.Add(tmp);

                }

            }//using end

            txtUserIDSetting.Text = this.Settings[0][0];
            txtPasswordSetting.Text = this.Settings[0][1];
            txtUserIDMaster.Text = this.Settings[1][0];
            txtPasswordMaster.Text = this.Settings[1][1];

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtUserIDSetting.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("User ID Setting is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if (txtPasswordSetting.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Password Setting is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if (txtUserIDMaster.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("User ID Master is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if (txtPasswordMaster.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Password Master is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            //CSV Encoding
            System.Text.Encoding enc =
                System.Text.Encoding.UTF8;

            System.IO.StreamWriter sr =
                new System.IO.StreamWriter(csvPartFile, false, enc);

            sr.WriteLine(txtUserIDSetting.Text.Replace(",", "") + "," + txtPasswordSetting.Text.Replace(",", ""));
            sr.WriteLine(txtUserIDMaster.Text.Replace(",", "") + "," + txtPasswordMaster.Text.Replace(",", ""));

            sr.Close();

            MessageBox.Show(this, objLanguage.Translate("Registration is Complete."),
                                       objLanguage.Translate("Information"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Information,
                                       MessageBoxDefaultButton.Button1, 0);

            this.Close();

        }

        private void txtUserIDSetting_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void txtPasswordSetting_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void txtUserIDMaster_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void txtPasswordMaster_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }
    }
}
