using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using XYY.Core.Standard.Mvc;
using System.Linq;
using System.Collections.Generic;
using XYY.Model.Standard.OrderAddressCheck;
using Microsoft.Extensions.Caching.Distributed;
using XYY.Data.Standard.OrderAddressCheck;

namespace XYY.Api.OrderAdderCheck.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UPSCheckController : ApiControllerBase
    {
        string cachekey = "UPSSLAreas";
        private IDistributedCache _cache;
        private IUPSAreasRepository _UPSAreasRepository;
        public async Task<List<UPSSLAreas>> getAreasAsCache()
        {

            var areas = await _cache.GetStringAsync(cachekey);
            if (areas == null || string.IsNullOrEmpty(areas))
            {
                var list = await _UPSAreasRepository.GetAll();
                areas = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                await _cache.SetStringAsync(cachekey, areas);
            }
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UPSSLAreas>>(areas);
            return data;
        }

        public UPSCheckController(IDistributedCache cache, IUPSAreasRepository uPSAreasRepository)
        {
            _cache = cache;
            _UPSAreasRepository = uPSAreasRepository;
        }

        public async Task<IActionResult> Check(string ZipCode, decimal length, decimal width, decimal height, decimal weight)
        {
            //超长判断
            string message = "";
            decimal zc = 0;
            decimal[] cc = new decimal[] { length, width, height }.OrderByDescending(x => x).ToArray();
            zc = cc[0] + 2 * cc[1] + 2 * cc[2];
            if (cc[0] > 120.0m ||
                cc[1] > 75m ||
               (zc > 266.0m && zc < 330.0m))
            {
                message = $"超尺寸附加费：预计费用15.75-20.5，最长边超过120厘米，第二长边超过75厘米,长+2*宽+2*高大于266厘米小于330厘米，当前传值{cc[0]}*{cc[1]}*{cc[2]}(cm)周长{zc}(cm)";
            }
            if (cc[0] > 244.0m ||
                cc[1] > 244.0m ||
                cc[2] > 244.0m ||
                (zc > 330.0m
                && zc < 419.0m)
                )
            {
                message = $"超大附加费：预计费用135-165，任意边长超过244厘米或长+2*（高+宽）超过330厘米小于419厘米，当前传值{cc[0]}*{cc[1]}*{cc[2]}(cm)周长{zc}(cm)";
            }

            if (
                cc[0] > 274.0m ||
                cc[1] > 274.0m ||
                cc[2] > 274.0m ||
                zc > 419.0m ||
                weight > 67.95m
                )
            {
                message = $"超大件限制：预计费用1025,任意边长超过274厘米或长+2*（高+宽）超过419厘米，或者单件超过150磅，当前传值{cc[0]}*{cc[1]}*{cc[2]}(cm)周长{zc}(cm) {weight}(kg)";

            }
            if (weight > 23.103m)
            {
                message += $";超重附加费：预计费用12,单件超过50磅";
            }


            var areas = await getAreasAsCache();
            if (ZipCode.Length < 5)
                ZipCode = ZipCode.PadLeft(5, '0');
            var area = areas.FirstOrDefault(x => x.ZipCode5 == ZipCode);
            if (area != null)
            {
                if (area.AreaName == "偏远")
                {
                    message += ";住宅地址偏远:预计费用 4.8/件";
                }
                else if (area.AreaName == "超偏远")
                {
                    message += ";住宅地址超偏远:预计费用 6.5/件";
                }
            }
            if (!string.IsNullOrEmpty(message))
            {
                message = message.Trim(';');
            }

            return Ok(message);
        }


    }
}
