using BellwetherBackend.Utility;
using System;
using System.Windows.Controls;

namespace BellwetherBackend.SubPage
{
    /// <summary>
    /// Interaction logic for UcBasicSetting.xaml
    /// </summary>
    public partial class UcBasicSetting : UserControl
    {
        private Utility.SettingJson settingJson;

        public UcBasicSetting()
        {
            InitializeComponent();

            LoadSettingData();
        }

        private void LoadSettingData()
        {
            settingJson = Setting.settingJsonFile;

            uxBackHomeSecText.Text = settingJson.BackHomeSec.ToString();
        }

        private void SaveData()
        {
            Setting.settingJsonFile = settingJson;
        }

        private void uxBackHomeSecText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.IsLoaded)
            {
                uxBackHomeSecText.TextChanged -= uxBackHomeSecText_TextChanged;
                if (Int32.TryParse(uxBackHomeSecText.Text, out int result))
                {
                    settingJson.BackHomeSec = result;
                    SaveData();
                }
                else
                {
                    uxBackHomeSecText.Text = settingJson.BackHomeSec.ToString();
                }
                uxBackHomeSecText.TextChanged += uxBackHomeSecText_TextChanged;
            }
        }
    }
}
