using System;
using System.Windows.Forms;
using System.Data.OleDb;
using FastReport.Utils;
using FastReport.Forms;

namespace FastReport.Data.ConnectionEditors
{
    internal partial class MsAccessConnectionEditor : ConnectionEditorBase
    {
        private string filter;
        private string connectionString;

        private void Localize()
        {
            MyRes res = new MyRes("ConnectionEditors,Common");
            gbDatabase.Text = res.Get("Database");
            lblDatabase.Text = res.Get("DatabaseFile");
            lblUserName.Text = res.Get("UserName");
            lblPassword.Text = res.Get("Password");
            btnAdvanced.Text = Res.Get("Buttons,Advanced");
            lbProvider.Text = Res.Get("ConnectionEditors,Access,Provider");
        }

        public override void UpdateDpiDependencies()
        {
            base.UpdateDpiDependencies();
            tbDatabase.Image = this.GetImage(1);
        }

        private void tbDatabase_ButtonClick(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = filter;
                if (dialog.ShowDialog() == DialogResult.OK)
                    tbDatabase.Text = dialog.FileName;
            }
        }

        private void btnAdvanced_Click(object sender, EventArgs e)
        {
            using (AdvancedConnectionPropertiesForm form = new AdvancedConnectionPropertiesForm())
            {
                OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
                builder.BrowsableConnectionString = false;
                form.AdvancedProperties = builder;

                if (form.ShowDialog() == DialogResult.OK)
                    ConnectionString = form.AdvancedProperties.ToString();
            }
        }

        private void CbxProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbxProvider.SelectedIndex)
            {
                case 0:
                    filter = Res.Get("FileFilters,MdbFile");
                    break;
                case 1:
                    filter = Res.Get("FileFilters,MdbFile") + "|" + Res.Get("FileFilters,AccdbFile");
                    break;
            }
        }

        protected override string GetConnectionString()
        {
            OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(connectionString);
            builder.DataSource = tbDatabase.Text;

            builder.Provider = cbxProvider.SelectedItem.ToString();

            if (!String.IsNullOrEmpty(tbUserName.Text))
                builder.Add(MsAccessDataConnection.strUserID, tbUserName.Text);
            else
                builder.Remove(MsAccessDataConnection.strUserID);

            if (!String.IsNullOrEmpty(tbPassword.Text))
                builder.Add(MsAccessDataConnection.strPassword, tbPassword.Text);
            else
                builder.Remove(MsAccessDataConnection.strPassword);

            return builder.ToString();
        }

        protected override void SetConnectionString(string value)
        {
            connectionString = value;
            OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(value);
            tbDatabase.Text = builder.DataSource;

            if(builder.Provider != "")
                cbxProvider.SelectedText = builder.Provider;

            object userName;
            builder.TryGetValue(MsAccessDataConnection.strUserID, out userName);
            tbUserName.Text = userName == null ? "" : userName.ToString();

            object password;
            builder.TryGetValue(MsAccessDataConnection.strPassword, out password);
            tbPassword.Text = password == null ? "" : password.ToString();
        }

        public MsAccessConnectionEditor()
        {
            InitializeComponent();
            Localize();
            CbxProvider_SelectedIndexChanged(null, null);
        }

    }
}

