using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;

namespace DragonVideoPlayer
{
    public class config_class
    {
        public static String config_text_read(String keyword)
        {


            string[] keys = null;
            String config_text = "";
            try
            {
                NameValueCollection appSettings = ConfigurationManager.AppSettings;
              

                keys = appSettings.GetValues(keyword);
                if (keys == null)
                {
                    error.write_error_log(keyword + "がconfig_text_readでnullエラー。");

                    Debug.WriteLine(keyword + "がnull");
                    return "";
                }
                config_text = keys[0].ToString();

            }
            catch (Exception ex)
            {
                error.write_error_log(ex.Message + ex.StackTrace);

                Debug.WriteLine(keyword + " " + ex.Message + ex.StackTrace);



            }
            return config_text;
        }

        public static void save_config_file(String name, String value)
        {

            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                System.Configuration.AppSettingsSection appSettings = config.AppSettings;



                appSettings.Settings[name].Value = value;





                config.Save(ConfigurationSaveMode.Modified);


                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (System.Exception ex)
            {
                error.write_error_log(ex.Message + ex.StackTrace);

                Debug.WriteLine(ex.Message + ex.StackTrace);
            }

        }



        public static int config_int_read(String keyword)
        {
         
           
            NameValueCollection appSettings = ConfigurationManager.AppSettings;

            string value = appSettings[keyword]; // ← こっち使う方が簡単

            if (string.IsNullOrWhiteSpace(value))
            {
              
                Debug.WriteLine(keyword + "が空");
                return 0; // デフォルト
            }
                if (int.TryParse(value, out int result))
                {
                    return result;
                }


          
          

            return 0; // パース

        }
        public static Boolean config_bool_read(String keyword)
        {
            
            string[] keys = null;
            Boolean config_bool = false;
            try
            {
                NameValueCollection appSettings = ConfigurationManager.AppSettings;


                keys = appSettings.GetValues(keyword);
                if (keys == null)
                {
                    error.write_error_log(keyword+ "がconfig_bool_readでnullエラー。");

                    Debug.WriteLine(keyword + "がnull");
                    return config_bool;
                }
                config_bool = bool_to_text.to_bool(keys[0].ToString());
               
            }
            catch (Exception ex)
            {
                error.write_error_log(ex.Message + ex.StackTrace);


                Debug.WriteLine(keyword + ":" + ex.Message + ex.StackTrace);

            }
            return config_bool;
        }

    }
}
