using System;
using System.Windows.Forms;
using CRVM.Entity;
using System.Text.RegularExpressions;
using System.IO;

namespace CRVM
{
    public partial class SetDataStorageForm : Form
    {
        public SetDataStorageForm()
        {
            InitializeComponent();
        }

        private void SetDataStorageForm_Load(object sender, EventArgs e)
        {
            InitalSetting();
        }

        private void InitalSetting()
        {
            checkBox_txtType.Checked = SysParam.Instance.checkTXT;
            checkBox_datType.Checked = SysParam.Instance.checkBIT;
            textBox_savePath.Text = SysParam.Instance.savePath;
            textBox_alarmSaveDays.Text = SysParam.Instance.saveDaysAlarm.ToString();
            textBox_noAlarmSaveDays.Text = SysParam.Instance.saveDaysNoAlarm.ToString();
        }

        private void btn_selectSavePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            if (folderBrowser.ShowDialog(this) == DialogResult.OK)
            {
                textBox_savePath.Text = folderBrowser.SelectedPath;
            }
        }

        private void checkBox_txtType_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_txtType.Checked)
            {
                checkBox_datType.Checked = false;
            }
            else
            {
                checkBox_datType.Checked = true;
            }
        }

        private void checkBox_datType_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_datType.Checked)
            {
                checkBox_txtType.Checked = false;
            }
            else
            {
                checkBox_txtType.Checked = true;
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            string pattern = @"^[1-9]\d*$";
            if (!Regex.IsMatch(textBox_alarmSaveDays.Text.Trim(), pattern))
            {
                MessageBox.Show("参数：存储时间（有报警）设置格式不正确，需为正整数！");
                return;
            }
            if (!Regex.IsMatch(textBox_noAlarmSaveDays.Text.Trim(), pattern))
            {
                MessageBox.Show("参数：存储时间（无报警）设置格式不正确，需为正整数！");
                return;
            }
            if (!Directory.Exists(textBox_savePath.Text.Trim()))
            {
                MessageBox.Show("存储目录不存在，请重新选择！");
                return;
            }
            if (!checkBox_datType.Checked && !checkBox_txtType.Checked)
            {
                MessageBox.Show("请选择文件的存储格式!");
                return;
            }

            SysParam.Instance.checkTXT = checkBox_txtType.Checked;
            SysParam.Instance.checkBIT = checkBox_datType.Checked;
            SysParam.Instance.savePath = textBox_savePath.Text;
            SysParam.Instance.saveDaysAlarm = Convert.ToInt32(textBox_alarmSaveDays.Text);
            SysParam.Instance.saveDaysNoAlarm = Convert.ToInt32(textBox_noAlarmSaveDays.Text);

            string driveStr = textBox_savePath.Text.Trim().Substring(0, 2);
            System.IO.DriveInfo drive = new System.IO.DriveInfo(driveStr);
            long availabelfreespace = drive.AvailableFreeSpace / 1024 / 1024 / 1024;//转换为G
            long avaliableDays = (availabelfreespace - 10) / 9;//每天5G数据，计算当前可以存储的天数,留10个G的冗余
            int maxSaveDays = SysParam.Instance.saveDaysNoAlarm > SysParam.Instance.saveDaysAlarm ? SysParam.Instance.saveDaysNoAlarm : SysParam.Instance.saveDaysAlarm;
            if (maxSaveDays > avaliableDays)
            {
                MessageBox.Show("当前硬盘剩余空间最多可存储" + avaliableDays + "天的数据，请重新设置存储时间!");
                return;
            }

            DialogResult dr = MessageBox.Show("系统将自动删除" + textBox_alarmSaveDays.Text + "天之前的报警数据！\r\n自动删除" + textBox_noAlarmSaveDays.Text + "天之前的不包含报警的数据！", "数据存储", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                ConfigHelper.GetInstance("").SaveConfig(Application.StartupPath + "\\AppSetting.config");
                this.Close();
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
