using Sunny.UI.Win32;
using System;
using System.Windows.Forms;

namespace SLC1_N
{
    public partial class Form_Save : Form
    {
        public Form_Save()
        {
            InitializeComponent();
        }

        private void Save_Load(object sender, EventArgs e)
        {
            if (Form1.f1.User != "管理员")
            {
                chk_CSV.Enabled = false;
            }

            var config = new JsonConfig("config.json");

            chk_TXT.Checked   = config.GetValue<bool>("TXT", true);
            chk_Excel.Checked = config.GetValue<bool>("Excel", false);
            chk_CSV.Checked   = config.GetValue<bool>("CSV", false);
            tb_path.Text      = config.GetValue<string>("FilePath", "");
        }

        // 选择文件夹
        private void BtnPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog1.ShowDialog();
            tb_path.Text = FolderBrowserDialog1.SelectedPath;
        }

        // 应用设置
        private void Use_Set_Click(object sender, EventArgs e)
        {
            var config = new JsonConfig("config.json");

            config.SetValue("TXT", chk_TXT.Checked);
            config.SetValue("Excel", chk_Excel.Checked);
            config.SetValue("CSV", chk_CSV.Checked);
            config.SetValue("FilePath", tb_path.Text);

            Form1.f1.filesave.TXT = chk_TXT.Checked;
            Form1.f1.filesave.Excel = chk_Excel.Checked;
            Form1.f1.filesave.CSV = chk_CSV.Checked;
            Form1.f1.filesave.Path = tb_path.Text;

            this.Close();
        }

        public void OpenForm(System.Windows.Forms.Form frm)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in System.Windows.Forms.Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    f.Activate();
                    f.Show();
                    frm.Dispose();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            frm.Show();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }

        private void Warning_Click(object sender, EventArgs e)
        {
            Warning wa = new Warning();
            OpenForm(wa);
        }
    }

    public class FileSave
    {
        public string Path { get; set; }
        public bool Excel { get; set; }
        public bool CSV { get; set; }
        public bool TXT { get; set; }
    }
}
