using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Formats.Asn1;
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 FrmSettingLogIn : Form
    {
        private string strMenu = string.Empty;
        private List<List<string>> Settings = new List<List<string>>();
        private string csvSettingLogIn = System.IO.Directory.GetCurrentDirectory() + "\\Settings.sys";
        private string loginUser = string.Empty;
        public dynamic objLanguage;

        public FrmSettingLogIn(string strMenu, string loginUser)
        {
            InitializeComponent();
            this.strMenu = strMenu;
            this.loginUser = loginUser;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmSettingLogIn_Load(object sender, EventArgs e)
        {
            if (!File.Exists(csvSettingLogIn))
            {
                using (StreamWriter sw = File.CreateText(csvSettingLogIn))
                {
                    sw.WriteLine("abc,123");
                    sw.WriteLine("abc,123");
                }
            }

            string strLogin = "Login";
            string strUser = "User";
            string strPassword = "Password";
            string strOK = "OK";
            string strCancel = "Cancel";

            lblLogin.Text = objLanguage.Translate(strLogin);
            lblUser.Text = objLanguage.Translate(strUser);
            lblPassword.Text = objLanguage.Translate(strPassword);
            btnOK.Text = objLanguage.Translate(strOK);
            btnCancel.Text = objLanguage.Translate(strCancel);


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

                }

            }//using end

            txtUser.Focus();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (strMenu == "Setting")
            {
                if ((txtUser.Text == this.Settings[0][0]) && (txtPassword.Text == this.Settings[0][1]))
                {
                    FrmSetting frmSetting = new FrmSetting();

                    this.Dispose();

                    frmSetting.ShowDialog();
                }
                else
                {
                    MessageBox.Show(this, objLanguage.Translate("Login Not correct"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                }
            }
            else if (strMenu == "MasterNew")
            {
                if ((txtUser.Text == this.Settings[1][0]) && (txtPassword.Text == this.Settings[1][1]))
                {
                    FrmMasterMaintenanceNew frmMasterMaintenanceNew = new FrmMasterMaintenanceNew(loginUser);

                    this.Dispose();

                    frmMasterMaintenanceNew.ShowDialog();
                }
                else
                {
                    MessageBox.Show(this, objLanguage.Translate("Login Not correct"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                }
            }
            else if (strMenu == "MasterEdit")
            {
                if ((txtUser.Text == this.Settings[1][0]) && (txtPassword.Text == this.Settings[1][1]))
                {

                    OpenFileDialog openFileDialog1 = new OpenFileDialog();
                    openFileDialog1.Filter = "Master file (*.003)|*.003|Working (*.000)|*.000";
                    //openFileDialog1.Filter = "Working (*.000)|*.000|Completion (*.001)|*.001|Error (*.002)|*.002|Master file (*.003)|*.003";
                    DialogResult result = openFileDialog1.ShowDialog();

                    if (result == DialogResult.OK)
                    {

                        FrmMasterMaintenanceDetail frmMasterMaintenanceDetail = new FrmMasterMaintenanceDetail(openFileDialog1.FileName, loginUser);

                        this.Dispose();

                        frmMasterMaintenanceDetail.ShowDialog();

                    }

                }
                else
                {
                    MessageBox.Show(this, objLanguage.Translate("Login Not correct"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                }
            }


        }

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