using Tiobon.Core.OPS.Tool.OPS.Tool.Helper;
using Tiobon.Core.OPS.Tool.OPS.Tool.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tiobon.Core.OPS.Tool.OPS.Tool.View
{
    public partial class Frm_MultiItem : Form
    {
        MultiEnvInfo item;
        bool b_update = false;
        public Frm_MultiItem(string name = null)
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(name))
            {
                b_update = true;
                this.Text = $"编辑 - {name}";
                item = Const.MultiEnvs.Where(o => o.Ip == Const.config.ip && o.Name == name).FirstOrDefault();
                if (!(item is null))
                {
                    this.txt_Name.Text = item.Name;
                    this.txt_FileName.Text = item.FileName;
                    this.txt_DefaultPort.Text = item.DefaultPort;
                    this.txt_DefaultMask.Text = item.DefaultMask;
                }
            }
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            foreach (var control in gb_main.Controls)
            {
                if (control is TextBox)
                {
                    var textBox = control as TextBox;
                    if (string.IsNullOrEmpty(textBox.Text.Trim()))
                    {
                        MessageBox.Show($"{textBox.Name}，不允许为空！", "提示");
                        return;
                    }
                }
            }

            int.TryParse(this.txt_DefaultPort.Text.Trim(), out int port);
            if (port < 100 || port > 655)
            {
                MessageBox.Show($"端口配置不正确，取值范围[100,655)！", "提示");
                return;
            }
            int.TryParse(this.txt_DefaultMask.Text.Trim(), out int mask);
            if (mask < 1 || mask > 255)
            {
                MessageBox.Show($"掩码配置不正确，取值范围[1,255)！", "提示");
                return;
            }
            var multiEnv = new MultiEnvInfo
            {
                Name = this.txt_Name.Text.Trim(),
                FileName = this.txt_FileName.Text.Trim(),
                Ip = Const.config.ip,
                DefaultPort = this.txt_DefaultPort.Text.Trim(),
                DefaultMask = this.txt_DefaultMask.Text.Trim()
            };
            if (b_update)
            {
                if (item.Name != multiEnv.Name)
                {
                    //新增
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.Name == multiEnv.Name))
                    {
                        MessageBox.Show($"环境名称已存在！", "提示");
                        return;
                    }
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.FileName == multiEnv.FileName))
                    {
                        MessageBox.Show($"文件名称已存在！", "提示");
                        return;
                    }
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.DefaultPort == multiEnv.DefaultPort))
                    {
                        MessageBox.Show($"端口已存在！", "提示");
                        return;
                    }
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.DefaultMask == multiEnv.DefaultMask))
                    {
                        MessageBox.Show($"子网掩码已存在！", "提示");
                        return;
                    }
                    Const.MultiEnvs.Add(multiEnv);
                }
                else
                {
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.Name != multiEnv.Name && o.FileName == multiEnv.FileName))
                    {
                        MessageBox.Show($"文件名称已存在！", "提示");
                        return;
                    }
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.Name != multiEnv.Name && o.DefaultPort == multiEnv.DefaultPort))
                    {
                        MessageBox.Show($"端口已存在！", "提示");
                        return;
                    }
                    if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.Name != multiEnv.Name && o.DefaultMask == multiEnv.DefaultMask))
                    {
                        MessageBox.Show($"子网掩码已存在！", "提示");
                        return;
                    }
                    //编辑
                    Const.MultiEnvs.Remove(item);
                    Const.MultiEnvs.Add(multiEnv);
                }
            }
            else
            {
                if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.Name == multiEnv.Name))
                {
                    MessageBox.Show($"环境名称已存在！", "提示");
                    return;
                }
                if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.FileName == multiEnv.FileName))
                {
                    MessageBox.Show($"文件名称已存在！", "提示");
                    return;
                }
                if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.DefaultPort == multiEnv.DefaultPort))
                {
                    MessageBox.Show($"端口已存在！", "提示");
                    return;
                }
                if (Const.MultiEnvs.Any(o => o.Ip == Const.config.ip && o.DefaultMask == multiEnv.DefaultMask))
                {
                    MessageBox.Show($"子网掩码已存在！", "提示");
                    return;
                }
                Const.MultiEnvs.Add(multiEnv);
            }
            Const.SaveMultiEnv();
            this.Close();
        }


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