using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.GrupUniApi.Config;
using CK.SCP.Utils;
using CK.SCP.Common;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;


namespace CK.SCP.GrupUniApi
{
    public partial class PopupSetting : Office2007Form
    {

        private List<GroupPanel> _groupList = new List<GroupPanel>();

        private void FormSetting_Load(object sender, EventArgs e)
        {
            ProjectName projectName;
//            GetConfig<CommonConfig>(GlobalConfig.CommonFileName);
            GetConfig<DbSetting>(GlobalConfig.ScpDbFileName);
            GetConfig<UniApiConfig>(GlobalConfig.UniApiConfigFileName);
            GetConfig<DurationConfig>(GlobalConfig.DurationFileName);

            Enum.TryParse(ScpCache.Config.项目名称, true, out projectName);



            switch (projectName)
            {
             
                case ProjectName.锦州锦恒:
                    GetConfig<DbSetting>(GlobalConfig.UniApiDbFileName);
                    break;
            }
            listSetting.SelectedIndex = 0;

        }
        private void GetConfig<T>(string filename) where T : new()
        {
            var group = SettingHelper.GetConfigValues<T>(filename);
            pnlMain.Controls.Add(@group);
            pnlMain.ResumeLayout();
            _groupList.Add(@group);
            listSetting.Items.Add(SettingHelper.GetPureName(filename));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                if (string.IsNullOrEmpty(txtSuperPassword.Text))
                    throw new Exception("请输入管理员密码!");
                if (txtSuperPassword.Text != GlobalVar.SuperPassword)
                    throw new Exception("管理员密码密码错误,请重新输入!");
                ProjectName projectName;
                Enum.TryParse(ScpCache.Config.项目名称, true, out projectName);
//                GlobalVar.CommonConfig = SettingHelper.SetConfigValues<CommonConfig>(_groupList, GlobalConfig.CommonFileName);
                GlobalConfig.ScpDatabase = SettingHelper.SetConfigValues<DbSetting>(_groupList, GlobalConfig.ScpDbFileName);
                GlobalVar.ApiConfig = SettingHelper.SetConfigValues<UniApiConfig>(_groupList, GlobalConfig.UniApiConfigFileName);
                GlobalVar.DurationConfig = SettingHelper.SetConfigValues<DurationConfig>(_groupList, GlobalConfig.DurationFileName);
                switch (projectName)
                {
                    case ProjectName.锦州锦恒:
                        GlobalConfig.UniApiDatabase = SettingHelper.SetConfigValues<DbSetting>(_groupList, GlobalConfig.UniApiDbFileName);
                        break;
                }

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageHelper.ShowError(ex);
                txtSuperPassword.Focus();
            }
        }

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

        private void listSetting_ItemClick(object sender, EventArgs e)
        {

        }

        private void listSetting_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listSetting.SelectedIndex < 0) return;
            var selectedName = listSetting.SelectedItem.ToString();
            var group = _groupList.SingleOrDefault(p => p.Name == selectedName);
            if (group == null) return;
            foreach (var groupPanel in _groupList)
            {
                groupPanel.Visible = false;
            }
            group.Visible = true;
        }
    }
}