using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using Models;
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HighWayIot.Winform.UserControlPages.RecipeConfigPages
{
    public partial class AddWeightForm : Form
    {
        /// <summary>
        /// 物料子类型服务类实例
        /// </summary>
        ZxMaterialChildTypeService zxMaterialChildTypeService = ZxMaterialChildTypeService.Instance;

        /// <summary>
        /// 物料类型服务类实例
        /// </summary>
        ZxMaterialTypeService zxMaterialTypeService = ZxMaterialTypeService.Instance;

        /// <summary>
        /// 物料服务类实例
        /// </summary>
        ZxMaterialService zxMaterialService = ZxMaterialService.Instance;

        /// <summary>
        /// 称量服务类实例
        /// </summary>
        ZxWeightService zxWeightService = ZxWeightService.Instance;

        /// <summary>
        /// 物料编号数组
        /// </summary>
        string[] MaterialCodeArray;

        /// <summary>
        /// 物料名称数组
        /// </summary>
        string[] MaterialNameArray;

        /// <summary>
        /// 物料子类型数组
        /// </summary>
        string[] MaterialChildTypeArray;

        /// <summary>
        /// 物料类型数组
        /// </summary>
        string[] MaterialTypeArray;

        /// <summary>
        /// 配方编号
        /// </summary>
        string RecipeCode; 

        public ZxWeightEntity OutValue { get; set; } = new ZxWeightEntity();

        public AddWeightForm(string recipeCode)
        {
            InitializeComponent();
            RecipeCode = recipeCode;
            Init();
        }

        private void Init()
        {
            RecipeCodeTextBox.Text = DateTime.Now.ToString("yyyyMMddHHmmss");

            //获取数据
            MaterialTypeArray = zxMaterialTypeService.GetMaterialTypeInfos().Select(x => x.MaterialTypeName).ToArray();
            List<ZxMaterialEntity> list = zxMaterialService.GetMaterialInfos();
            MaterialCodeArray = list.Select(x => x.MaterialCode).ToArray();
            MaterialNameArray = list.Select(x => x.MaterialName).ToArray();
            string[] materialType = new string[4]
            {
                "",
                "基部胶",
                "缓冲胶",
                "胎面胶"
            };

            //添加空白行
            MaterialTypeArray = GeneralUtils.HeadAddEmptyString(MaterialTypeArray);
            MaterialCodeArray = GeneralUtils.HeadAddEmptyString(MaterialCodeArray);
            MaterialNameArray = GeneralUtils.HeadAddEmptyString(MaterialNameArray);

            //绑定combobox数据源
            MaterialTypeComboBox.DataSource = MaterialTypeArray;
            MaterialCodeComboBox.DataSource = MaterialCodeArray;
            MaterialNameComboBox.DataSource = MaterialNameArray;
            RubTypeCombobox.DataSource = materialType;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(MaterialCodeComboBox.Text))
            {
                MessageBox.Show("物料编号不可为空！");
                return;
            }

            ZxWeightEntity zxWeightEntity = new ZxWeightEntity()
            {
                RecipeCode = this.RecipeCode,
                MaterialCode = MaterialCodeComboBox.Text.Trim(),
                MaterialName = MaterialNameComboBox.Text.Trim(),
                MaterialType = RubTypeCombobox.Text.Trim(),
                IsUse = IsUseCheckBox.Checked,
                IsDeleted = false,
            };

            if (string.IsNullOrEmpty(RubTypeCombobox.Text))
            {
                MessageBox.Show("请选择胶料位置类型！");
                return;
            }

            var lists = zxWeightService.GetWeightInfos(RecipeCode);

            if (lists.Where(x => x.MaterialCode == MaterialCodeComboBox.Text.Trim()).Count() >= 1)
            {
                MessageBox.Show("同一配方不能出现两种物料相同的称量信息！");
                return;
            }

            if (!decimal.TryParse(SetThicknessTextBox.Text.Trim(), out decimal thickness) && !string.IsNullOrEmpty(SetThicknessTextBox.Text.Trim()))
            {
                MessageBox.Show("厚度请填入可带小数的阿拉伯数字");
                return;
            }
            zxWeightEntity.SetThickness = thickness;

            if (!decimal.TryParse(SetWidthTextBox.Text.Trim(), out decimal width) && !string.IsNullOrEmpty(SetWidthTextBox.Text.Trim()))
            {
                MessageBox.Show("宽度请填入可带小数的阿拉伯数字");
                return;
            }
            zxWeightEntity.SetWidth = width;

            if (!decimal.TryParse(SetWidthTextBox2.Text.Trim(), out decimal width2) && !string.IsNullOrEmpty(SetWidthTextBox2.Text.Trim()))
            {
                MessageBox.Show("宽度请填入可带小数的阿拉伯数字");
                return;
            }
            zxWeightEntity.SetWidth2 = width2;

            if (!int.TryParse(SetLayerTextBox.Text.Trim(), out int layer) && !string.IsNullOrEmpty(SetLayerTextBox.Text.Trim()))
            {
                MessageBox.Show("层数请填入整数阿拉伯数字");
                return;
            }
            zxWeightEntity.SetLayer = layer;

            if (!int.TryParse(SetLayerTextBox2.Text.Trim(), out int layer2) && !string.IsNullOrEmpty(SetLayerTextBox2.Text.Trim()))
            {
                MessageBox.Show("层数请填入整数阿拉伯数字");
                return;
            }
            zxWeightEntity.SetLayer2 = layer2;

            if (!decimal.TryParse(SetWeightTextBox.Text.Trim(), out decimal weight) && !string.IsNullOrEmpty(SetWeightTextBox.Text.Trim()))
            {
                MessageBox.Show("重量请填入可带小数的阿拉伯数字");
                return;
            }
            zxWeightEntity.SetWeight = weight;

            if (!decimal.TryParse(SetErrorTextBox.Text.Trim(), out decimal error)&& !string.IsNullOrEmpty(SetErrorTextBox.Text.Trim()))
            {
                MessageBox.Show("重量公差请填入可带小数的阿拉伯数字");
                return;
            }
            zxWeightEntity.SetError = error;

            if (zxWeightService.InsertWeightInfo(zxWeightEntity))
            {
                MessageBox.Show("成型信息添加成功！");
            }
            else
            {
                MessageBox.Show("成型信息添加失败！请尝试重新添加或检查数据库连接");
                return;
            }
            OutValue = zxWeightEntity;

            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 物料类型选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialTypeComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            List<ZxMaterialEntity> list = zxMaterialService.GetMaterialInfos(null, MaterialTypeComboBox.Text.Trim());
            MaterialCodeArray = list.Select(x => x.MaterialCode).ToArray();
            MaterialNameArray = list.Select(x => x.MaterialName).ToArray();

            //添加空白行
            MaterialCodeArray = GeneralUtils.HeadAddEmptyString(MaterialCodeArray);
            MaterialNameArray = GeneralUtils.HeadAddEmptyString(MaterialNameArray);

            //绑定combobox数据源
            MaterialCodeComboBox.DataSource = MaterialCodeArray;
            MaterialNameComboBox.DataSource = MaterialNameArray;

            MaterialChildTypeArray = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == MaterialTypeComboBox.Text).Select(x => x.MaterialChlidTypeName).ToArray();
            MaterialChildTypeArray = GeneralUtils.HeadAddEmptyString(MaterialChildTypeArray);

            ChildTypeComboBox.DataSource = MaterialChildTypeArray;
        }

        /// <summary>
        /// 物料子类型选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChildTypeComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            List<ZxMaterialEntity> list = zxMaterialService.GetMaterialInfos(null, MaterialTypeComboBox.Text.Trim(), ChildTypeComboBox.Text.Trim());
            MaterialCodeArray = list.Select(x => x.MaterialCode).ToArray();
            MaterialNameArray = list.Select(x => x.MaterialName).ToArray();

            //添加空白行
            MaterialCodeArray = GeneralUtils.HeadAddEmptyString(MaterialCodeArray);
            MaterialNameArray = GeneralUtils.HeadAddEmptyString(MaterialNameArray);

            //绑定combobox数据源
            MaterialCodeComboBox.DataSource = MaterialCodeArray;
            MaterialNameComboBox.DataSource = MaterialNameArray;
        }

        /// <summary>
        /// 物料编号选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialCodeComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            List<ZxMaterialEntity> entitys = zxMaterialService.GetEntityByMaterialCode(MaterialCodeComboBox.Text.Trim());

            if (entitys == null && entitys.Count == 0)
            {
                return;
            }

            if (entitys.Count > 1)
            {
                MessageBox.Show("请检查是否有多条物料编号相同的可用物料");
                return;
            }

            ZxMaterialEntity entity = entitys[0];

            MaterialNameComboBox.Text = entity.MaterialName;
            MaterialTypeComboBox.Text = entity.MaterialType;
            ChildTypeComboBox.Text = entity.ChildType;
        }

        /// <summary>
        /// 物料名称选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialNameComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            List<ZxMaterialEntity> entitys = zxMaterialService.GetMaterialInfos(MaterialNameComboBox.Text.Trim());

            if (entitys.Count == 1)
            {
                ZxMaterialEntity entity = entitys[0];
                MaterialCodeComboBox.Text = entity.MaterialCode;
                MaterialTypeComboBox.Text = entity.MaterialType;
                ChildTypeComboBox.Text = entity.ChildType;
            }
            else if (entitys == null)
            {
                return;
            }
            else
            {
                MaterialCodeArray = entitys.Select(x => x.MaterialCode).ToArray();

                //添加空白行
                MaterialCodeArray = GeneralUtils.HeadAddEmptyString(MaterialCodeArray);

                //绑定combobox数据源
                MaterialCodeComboBox.DataSource = MaterialCodeArray;

                MaterialTypeComboBox.Text = string.Empty;
                ChildTypeComboBox.Text = string.Empty;
            }
        }

        /// <summary>
        /// 物料类型更改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaterialChildTypeArray = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == MaterialTypeComboBox.Text).Select(x => x.MaterialChlidTypeName).ToArray();
            MaterialChildTypeArray = GeneralUtils.HeadAddEmptyString(MaterialChildTypeArray);
            ChildTypeComboBox.DataSource = MaterialChildTypeArray;
        }

        private void AddWeightForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
