using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppConfigSetting
{
    public partial class AppConfigSetForm : Form
    {
        string key_IsDemoMesServer = "IsDemoMesServer";
        string key_ConnectionString_MES = "ConnectionString_MES";
        string key_Url_WebApiDT = "Url_WebApiDT";
        string key_Url_WebApi = "Url_WebApi";
        string key_MqttUrl = "MqttUrl";
        string key_MqttTargetTopicPLC = "MqttTargetTopic";
        
        public AppConfigSetForm()
        {
            InitializeComponent();
            ReadConfig();
        }

        private void button_Read_Click(object sender, EventArgs e)
        {
            ReadConfig();
        }
        private void ReadConfig()
        {
            textBox_IsDemoMesServer.Text = AppConfig.GetAppSettingsValue(key_IsDemoMesServer);
            textBox_ConnectionString_MES.Text = AppConfig.GetAppSettingsValue(key_ConnectionString_MES);
            textBox_Url_WebApiDT.Text = AppConfig.GetAppSettingsValue(key_Url_WebApiDT);
            textBox_Url_WebApi.Text = AppConfig.GetAppSettingsValue(key_Url_WebApi);
            textBox_MqttUrl.Text = AppConfig.GetAppSettingsValue(key_MqttUrl);
            textBox_MqttTargetTopicPLC.Text = AppConfig.GetAppSettingsValue(key_MqttTargetTopicPLC);
        }
        private void UdateConfig()
        {
            AppConfig.UpdateAppSettings(key_IsDemoMesServer, textBox_IsDemoMesServer.Text);
            AppConfig.UpdateAppSettings(key_ConnectionString_MES, textBox_ConnectionString_MES.Text);
            AppConfig.UpdateAppSettings(key_Url_WebApiDT, textBox_Url_WebApiDT.Text);
            AppConfig.UpdateAppSettings(key_Url_WebApi, textBox_Url_WebApi.Text);
            AppConfig.UpdateAppSettings(key_MqttUrl, textBox_MqttUrl.Text);
            AppConfig.UpdateAppSettings(key_MqttTargetTopicPLC, textBox_MqttTargetTopicPLC.Text);
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            UdateConfig();
        }
    }

    public class AppConfig
    {
        public static string GetAppSettingsValue(string key)
        {
            ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[key] ?? string.Empty;
        }
        public static bool UpdateAppSettings(string key, string value)
        {
            var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!_config.HasFile)
            {
                throw new ArgumentException("程序配置文件缺失！");
            }
            KeyValueConfigurationElement _key = _config.AppSettings.Settings[key];
            if (_key == null)
                _config.AppSettings.Settings.Add(key, value);
            else
                _config.AppSettings.Settings[key].Value = value;
            _config.Save(ConfigurationSaveMode.Modified);
            return true;
        }
    }
}
