using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Nirvana.Common;
using YBDevice.Application;
using YBDevice.Entity;

namespace YBDevice.NWeb.Pages.Result
{
    public class PrintModel : BaseModel
    {
        private readonly IResultService _resultService;
        public UserMeasureDetailDto data = new UserMeasureDetailDto();
        public List<string> weightlist = new List<string>();
        public List<decimal> musclevallist = new List<decimal>();//肌肉重量列表
        public List<decimal> musclelist = new List<decimal>();
        public List<decimal> fat_rlist = new List<decimal>();
        public List<decimal> fat_wlist = new List<decimal>();
        public List<decimal> waterlist = new List<decimal>();
        public List<decimal> skeletalmusclelist = new List<decimal>();
        public List<decimal> bonelist = new List<decimal>();
        public List<decimal> bmilist = new List<decimal>();
        public List<decimal> sfrlist = new List<decimal>();
        public List<decimal> sfrvallist = new List<decimal>();
        public List<decimal> viscerallist = new List<decimal>();


        public decimal weight_pro = 1;//体重条百分比
        public decimal muscleval_pro = 1;//肌肉重量条百分比
        public decimal muscle_pro = 1;//肌肉率条百分比
        public decimal fat_r_pro = 1;//脂肪率条
        public decimal fat_w_pro = 1;//脂肪重量条
        public decimal water_pro = 1;//水份条
        public decimal skeletalmuscle_pro = 1;//骨骼肌量条
        public decimal bone_pro = 1;//骨量条
        public decimal bmi_pro = 1;//BMI条
        public decimal sfr_pro = 1;//皮下脂肪率条
        public decimal sfrval_pro = 1;//皮下脂肪量条
        public decimal visceral_pro = 1;//内脂条

        public string musclevalscope = "-";//肌肉量范围
        public string musclescope = "-";//肌肉率范围
        public string fat_rscope = "-";//脂肪率范围
        public string fat_wscope = "-";//肌肉量范围
        public string waterscope = "-";//水分范围
        public string skescope = "-";//骨骼肌量范围
        public string bonescope = "-";//骨量范围
        public string bmiscope = "-";//bmi范围
        public string sfrscope = "-";//皮下脂肪率范围
        public string sfrvalscope = "-";//皮下脂肪量范围
        public string visceralscope = "-";//内脂范围
        public PrintModel(IResultService resultService)
        {
            _resultService = resultService;
        }
        public async Task OnGetAsync(Guid? id = null)
        {
            data = await _resultService.GetDetailAsync(id.Value);
            if (data != null)
            {
                //体重进度以理想体重为基准
                var maxweight = data.idealweight;
                decimal sweight = data.idealweight / 2;
                if (data.weight > data.idealweight)
                {
                    maxweight = data.idealweight - 2 * sweight <= 0 ? data.weight : data.idealweight - 2 * sweight;
                }
                else
                {
                    maxweight = data.idealweight + 3 * sweight;
                }
                weight_pro = CalcProgress(data.weight, maxweight);
                muscleval_pro = CalcProgress(data.muscleval, data.values.muscleval_value);
                muscle_pro = CalcProgress(data.muscle, data.values.muscle_value);
                fat_r_pro = CalcProgress(data.fat_r, data.values.fa_r_value);
                fat_w_pro = CalcProgress(data.fat_w, data.values.fat_w_value);
                water_pro = CalcProgress(data.water, data.values.water_value);
                bone_pro = CalcProgress(data.bone, data.values.bone_value);
                bmi_pro = CalcProgress(data.bmi, data.values.bmi_value);
                sfr_pro = CalcProgress(data.sfr, data.values.sfr_value);
                sfrval_pro = CalcProgress(data.sfrval, data.values.sfrval_value);
                skeletalmuscle_pro = CalcProgress(data.SkeletalMuscle, data.values.skeletalmusclekg_value);
                visceral_pro = CalcProgress(data.visceral, data.values.visceral_value);

                musclevallist = GetProcessList(data.values.muscleval_value);
                musclelist = GetProcessList(data.values.muscle_value);
                fat_rlist = GetProcessList(data.values.fa_r_value);
                fat_wlist = GetProcessList(data.values.fat_w_value);
                waterlist = GetProcessList(data.values.water_value);
                bonelist = GetProcessList(data.values.bone_value);
                bmilist = GetProcessList(data.values.bmi_value);
                sfrlist = GetProcessList(data.values.sfr_value);
                sfrvallist = GetProcessList(data.values.sfrval_value);
                viscerallist = GetProcessList(data.values.visceral_value);
                skeletalmusclelist = GetProcessList(data.values.skeletalmusclekg_value);

                musclevalscope = StandVal(data.values.muscleval_value);
                musclescope = StandVal(data.values.muscle_value);
                fat_rscope = StandVal(data.values.fa_r_value);
                fat_wscope = StandVal(data.values.fat_w_value);
                waterscope = StandVal(data.values.water_value);
                bonescope = StandVal(data.values.bone_value);
                bmiscope = StandVal(data.values.bmi_value);
                sfrscope = StandVal(data.values.sfr_value);
                sfrvalscope = StandVal(data.values.sfrval_value);
                visceralscope = StandVal(data.values.visceral_value);
                skescope = StandVal(data.values.skeletalmusclekg_value);
            }
        }


        /// <summary>
        /// 获取进度分度列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<decimal> GetProcessList(List<MeasureInfoItemValue> list)
        {
            List<decimal> lists = new List<decimal>();
            if (list.Count == 4)
            {
                foreach (var item in list)
                {
                    var minval = item.minvalue.ToDecimal(1);
                    var maxval = item.maxvalue.ToDecimal(1);
                    var index = list.IndexOf(item);
                    if (minval == 0)
                    {
                        minval = 1;
                    }
                    if (index == 0)
                    {
                        lists.Add(minval);
                    }
                    if (index == 1)
                    {
                        lists.Add(minval);
                        lists.Add(maxval);
                    }
                    if (index == list.Count - 1)
                    {
                        lists.Add(maxval);
                    }
                }
            }
            else
            {
                foreach (var item in list)
                {
                    var minval = item.minvalue.ToDecimal(1);
                    if (minval == 0)
                    {
                        minval = 1;
                    }
                    if (!lists.Contains(minval))
                    {
                        lists.Add(minval);
                    }
                    var maxval = item.maxvalue.ToDecimal(1);
                    if (!lists.Contains(maxval))
                    {
                        lists.Add(maxval);
                    }
                }
            }
            return lists;
        }

        /// <summary>
        /// 计算进度条占比
        /// </summary>
        /// <param name="val"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private decimal CalcProgress(decimal val, List<MeasureInfoItemValue> list)
        {
            decimal maxval = MaxVal(list);
            //取中间值
            decimal sminval = 1;
            decimal smaxval = 1;
            if (list.Count == 3)
            {
                var data = list.Skip(1).Take(1).FirstOrDefault();
                smaxval = data.maxvalue;
                sminval = data.minvalue;
            }
            else if (list.Count == 4)
            {
                var data = list.Skip(1).Take(1).FirstOrDefault();
                sminval = data.minvalue;
                data = list.Skip(2).Take(1).FirstOrDefault();
                smaxval = data.maxvalue;
            }
            if (val >= maxval)
            {
                return 100;
            }
            decimal returnval = 1;
            if (val > smaxval)
            {
                var v = (val - smaxval) / (maxval - smaxval) * 100 / 4;
                returnval = 60 + v;
                if (returnval > 100)
                    returnval = 100;
            }
            else if (val < sminval)
            {
                var v = val / sminval * 100;
                returnval = v * 0.4m;
            }
            else
            {
                var v = ((val - sminval) / (smaxval - sminval) * 100) / 2;
                returnval = 45 + v;
                if (returnval > 60)
                    returnval = 60;
            }
            return returnval.ToDecimal(2);
        }

        /// <summary>
        /// 计算进度条占比
        /// </summary>
        /// <param name="val">当前值</param>
        /// <param name="maxval">最大值</param>
        /// <returns></returns>
        private decimal CalcProgress(decimal val, decimal maxval)
        {
            var pval = (val / (maxval + 1)) * 100;
            if (pval <= 0)
            {
                return 1;
            }
            if (pval > 100)
            {
                pval = 100;
            }
            return pval.ToDecimal(1);
        }
        /// <summary>
        /// 获取列表最大值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private decimal MaxVal(List<MeasureInfoItemValue> list)
        {
            decimal maxval = 0;
            foreach (var item in list)
            {
                if (item.maxvalue > maxval)
                {
                    maxval = item.maxvalue;
                }
            }
            return maxval;
        }
        /// <summary>
        /// 获取标准范围
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private string StandVal(List<MeasureInfoItemValue> list)
        {
            if (list == null || list.Count == 0)
            {
                return "";
            }
            var data = list.Skip(1).Take(1).FirstOrDefault();
            return $"{data.minvalue}~{data.maxvalue}";
        }
    }
}
