using System;
using System.Text;
using System.Data;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Generic;

using BrnShop.Core;
using BrnShop.Services;
using BrnShop.Web.Framework;
using BrnShop.Web.Models;

namespace BrnShop.Web.Controllers
{
    /// <summary>
    /// 购物车控制器类
    /// </summary>
    public class ShopCartController : BaseWebController
    {
        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        public ActionResult AddProduct()
        {
            int pid = WebHelper.GetQueryInt("pid");//商品id
            int buyCount = WebHelper.GetQueryInt("buyCount");//购买数量
            int scSubmitType = WorkContext.ShopConfig.SCSubmitType;//购物车的提交方式

            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }) } }));
                    case 1:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }) } }));
                    case 2:
                        return Content("nologin");
                    default:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }) } }));
                }
            }

            //判断商品是否存在
            PartProductInfo partProductInfo = Products.GetPartProductById(pid);
            if (partProductInfo == null)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView("/", "商品不存在");
                    case 1:
                        return PromptView("/", "商品不存在");
                    case 2:
                        return Content("noproduct");
                    default:
                        return PromptView("/", "商品不存在");
                }
            }

            //购买数量不能小于1
            if (buyCount < 1)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "购买数量不能小于1");
                    case 1:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "购买数量不能小于1");
                    case 2:
                        return Content("buycountmin");
                    default:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "购买数量不能小于1");
                }
            }

            //商品库存
            int stockNumber = Products.GetProductStockNumberByPid(pid);
            if (stockNumber < buyCount)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "商品已售空");
                    case 1:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "商品已售空");
                    case 2:
                        return Content("stockout");
                    default:
                        return PromptView(Url.Action("Product", "Catalog", new RouteValueDictionary { { "pid", pid } }), "商品已售空");
                }
            }

            //购物车中已经存在的商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            if (Orders.GetCommonOrderProductByPid(pid, orderProductList) == null)
            {
                if ((WorkContext.Uid > 0 && orderProductList.Count >= WorkContext.ShopConfig.MemberSCCount) || (WorkContext.Uid < 1 && orderProductList.Count >= WorkContext.ShopConfig.GuestSCCount))
                {
                    switch (scSubmitType)
                    {
                        case 2:
                            return Content("toomany");
                        default:
                            return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 0 }, { "id", pid } });
                    }
                }
            }


            //将商品添加到购物车
            Orders.AddProductToShopCart(ref orderProductList, buyCount, partProductInfo, WorkContext.Sid, WorkContext.Uid, DateTime.Now, WorkContext.IP);
            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(orderProductList));

            switch (scSubmitType)
            {
                case 0:
                    return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 0 }, { "id", pid } });
                case 1:
                    return RedirectToAction("Index");
                case 2:
                    return Content("success");
                default:
                    return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 0 }, { "id", pid } });
            }
        }

        /// <summary>
        /// 修改购物车中商品数量
        /// </summary>
        public ActionResult ChangePruductCount()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pid = WebHelper.GetQueryInt("pid");//商品id
            int buyCount = WebHelper.GetQueryInt("buyCount");//购买数量

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //对应商品
            OrderProductInfo orderProductInfo = Orders.GetCommonOrderProductByPid(pid, orderProductList);

            if (orderProductInfo != null)//当商品已经存在
            {
                if (buyCount < 1)//当购买数量小于1时,删除此商品
                {
                    Orders.DeleteShopCartProduct(ref orderProductList, orderProductInfo);
                }
                else
                {
                    if (buyCount != orderProductInfo.BuyCount)
                        Orders.AddExistProductToShopCart(ref orderProductList, orderProductInfo, buyCount - orderProductInfo.BuyCount, WorkContext.Sid, WorkContext.Uid, DateTime.Now);
                }
            }

            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

            return View("AjaxIndex", model);
        }

        /// <summary>
        /// 删除购物车中商品
        /// </summary>
        public ActionResult DelPruduct()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pid = WebHelper.GetQueryInt("pid");//商品id

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //对应商品
            OrderProductInfo orderProductInfo = Orders.GetCommonOrderProductByPid(pid, orderProductList);

            if (orderProductInfo != null)
                Orders.DeleteShopCartProduct(ref orderProductList, orderProductInfo);

            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

            return View("AjaxIndex", model);
        }

        /// <summary>
        /// 添加套装到购物车
        /// </summary>
        public ActionResult AddSuit()
        {
            int pmId = WebHelper.GetQueryInt("pmId");//套装id
            int buyCount = WebHelper.GetQueryInt("buyCount");//购买数量
            int scSubmitType = WorkContext.ShopConfig.SCSubmitType;//购物车的提交方式

            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }) } }));
                    case 1:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }) } }));
                    case 2:
                        return Content("nologin");
                    default:
                        return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }) } }));
                }
            }

            //商城关闭套装促销活动
            if (BSPConfig.ShopConfig.IsSuitPromotion == 0)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView("/", "商城已经关闭了套装促销活动");
                    case 1:
                        return PromptView("/", "商城已经关闭了套装促销活动");
                    case 2:
                        return Content("closesuit");
                    default:
                        return PromptView("/", "商城已经关闭了套装促销活动");
                }
            }

            //购买数量不能小于1
            if (buyCount < 1)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView(Url.Action("Suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "购买数量不能小于1");
                    case 1:
                        return PromptView(Url.Action("Suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "购买数量不能小于1");
                    case 2:
                        return Content("buycountmin");
                    default:
                        return PromptView(Url.Action("Suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "购买数量不能小于1");
                }
            }

            //获得套装促销活动
            SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(pmId, DateTime.Now);
            //套装促销活动不存在时
            if (suitPromotionInfo == null)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView("/", "套装不存在");
                    case 1:
                        return PromptView("/", "套装不存在");
                    case 2:
                        return Content("nosuit");
                    default:
                        return PromptView("/", "套装不存在");
                }
            }

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            if (Orders.GetSuitOrderProductList(pmId, orderProductList).Count == 0)
            {
                if ((WorkContext.Uid > 0 && orderProductList.Count >= WorkContext.ShopConfig.MemberSCCount) || (WorkContext.Uid < 1 && orderProductList.Count >= WorkContext.ShopConfig.GuestSCCount))
                {
                    switch (scSubmitType)
                    {
                        case 2:
                            return Content("toomany");
                        default:
                            return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 1 }, { "id", pmId } });
                    }
                }
            }

            //套装商品列表
            DataTable suitProductList = Promotions.GetSuitProductList(suitPromotionInfo.PmId);
            if (suitProductList.Rows.Count < 1)
            {
                switch (scSubmitType)
                {
                    case 0:
                        return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "套装商品为空");
                    case 1:
                        return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "套装商品为空");
                    case 2:
                        return Content("noproduct");
                    default:
                        return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "套装商品为空");
                }
            }

            //套装商品id列表
            StringBuilder pidList = new StringBuilder();
            foreach (DataRow row in suitProductList.Rows)
                pidList.AppendFormat("{0},", row["pid"]);
            pidList.Remove(pidList.Length - 1, 1);

            //套装商品库存列表
            List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList.ToString());
            foreach (ProductStockInfo item in productStockList)
            {
                if (item.Number < buyCount)
                {
                    switch (scSubmitType)
                    {
                        case 0:
                            return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "库存不足");
                        case 1:
                            return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "库存不足");
                        case 2:
                            return Content(item.Pid.ToString());
                        default:
                            return PromptView(Url.Action("suit", "Catalog", new RouteValueDictionary { { "pmId", pmId } }), "库存不足");
                    }
                }
            }

            Orders.AddSuitToShopCart(ref orderProductList, suitProductList, suitPromotionInfo, buyCount, WorkContext.Sid, WorkContext.Uid, DateTime.Now, WorkContext.IP);
            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(orderProductList));

            switch (scSubmitType)
            {
                case 0:
                    return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 1 }, { "id", pmId } });
                case 1:
                    return RedirectToAction("Index");
                case 2:
                    return Content("success");
                default:
                    return RedirectToAction("AddSuccess", new RouteValueDictionary { { "type", 1 }, { "id", pmId } });
            }
        }

        /// <summary>
        /// 修改购物车中套装数量
        /// </summary>
        public ActionResult ChangeSuitCount()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pmId = WebHelper.GetQueryInt("pmId");//套装id
            int buyCount = WebHelper.GetQueryInt("buyCount");//购买数量

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //套装商品列表
            List<OrderProductInfo> suitOrderProductList = Orders.GetSuitOrderProductList(pmId, orderProductList);

            if (suitOrderProductList.Count > 0)//当套装已经存在
            {
                if (buyCount < 1)//当购买数量小于1时,删除此套装
                {
                    Orders.DeleteShopCartSuit(ref orderProductList, pmId);
                }
                else
                {
                    int oldBuyCount = suitOrderProductList[0].RealCount / suitOrderProductList[0].ExtCode9;
                    if (buyCount != oldBuyCount)
                        Orders.AddExistSuitToShopCart(ref orderProductList, suitOrderProductList, buyCount - oldBuyCount, DateTime.Now);
                }
            }

            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

            return View("AjaxIndex", model);
        }

        /// <summary>
        /// 删除购物车中套装
        /// </summary>
        public ActionResult DelSuit()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pmId = WebHelper.GetQueryInt("pmId");//套装id

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            Orders.DeleteShopCartSuit(ref orderProductList, pmId);

            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

            return View("AjaxIndex", model);
        }

        /// <summary>
        /// 添加满赠到购物车
        /// </summary>
        public ActionResult AddFullSend()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pmId = WebHelper.GetQueryInt("pmId");//满赠id
            int pid = WebHelper.GetQueryInt("pid");//商品id

            //添加的商品
            PartProductInfo partProductInfo = Products.GetPartProductById(pid);
            if (partProductInfo == null)
                return Content("noproduct");

            //商品库存
            int stockNumber = Products.GetProductStockNumberByPid(pid);
            if (stockNumber < 1)
                return Content("stockout");

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //赠送商品
            OrderProductInfo fullSendMinorOrderProductInfo = Orders.GetFullSendMinorOrderProduct(pmId, orderProductList);
            if (fullSendMinorOrderProductInfo != null)
            {
                return Content("exist");
            }
            else
            {
                //满赠主商品列表
                List<OrderProductInfo> fullSendMainOrderProductList = Orders.GetFullSendMainOrderProductList(pmId, orderProductList);
                if (fullSendMainOrderProductList.Count < 1)
                    return Content("nolimit");
                decimal amount = Orders.SumOrderProductAmount(fullSendMainOrderProductList);
                if (fullSendMainOrderProductList[0].ExtCode8 > amount)
                    return Content("nolimit");

                if (!Promotions.IsExistFullSendProduct(pmId, pid, 1))
                    return Content("nosendproduct");

                Orders.AddFullSendToShopCart(ref orderProductList, partProductInfo, fullSendMainOrderProductList[0].ExtCode7, fullSendMainOrderProductList[0].ExtCode9, WorkContext.Sid, WorkContext.Uid, DateTime.Now, WorkContext.IP);

                //商品总数量
                int totalCount = Orders.SumOrderProductCount(orderProductList);
                //商品合计
                decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
                //满减折扣
                decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
                //订单合计
                decimal orderAmount = productAmount - fullCutDiscount;
                //整理的订单商品列表
                List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCutDiscount = fullCutDiscount,
                    OrderAmount = orderAmount,
                    OrderProductList = tidiedOrderProductList
                };

                //将购物车中商品数量写入cookie
                Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

                return View("AjaxIndex", model);
            }
        }

        /// <summary>
        /// 删除购物车中满赠
        /// </summary>
        public ActionResult DelFullSend()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Content("nologin");

            int pmId = WebHelper.GetQueryInt("pmId");//满赠id

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //删除满赠
            Orders.DeleteShopCartFullSend(ref orderProductList, pmId);

            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(Orders.SumOrderProductCount(tidiedOrderProductList));

            return View("AjaxIndex", model);
        }

        /// <summary>
        /// 购物车添加成功提示
        /// </summary>
        public ActionResult AddSuccess()
        {
            int id = WebHelper.GetQueryInt("id");//项id
            int type = WebHelper.GetQueryInt("type");//项类型

            CartAddSuccessModel model = new CartAddSuccessModel
            {
                ItemId = id,
                ItemType = type
            };
            return View(model);
        }

        /// <summary>
        /// 购物车
        /// </summary>
        public ActionResult Index()
        {
            //当商城不允许游客使用购物车时
            if (WorkContext.ShopConfig.IsGuestSC == 0 && WorkContext.Uid < 1)
                return Redirect(Url.Action("login", "Account", new RouteValueDictionary { { "returnUrl", Url.Action("List") } }));

            //购物车商品列表
            List<OrderProductInfo> orderProductList = Orders.GetShopCartProductList(WorkContext.Uid, WorkContext.Sid);
            //商品总数量
            int totalCount = Orders.SumOrderProductCount(orderProductList);
            //商品合计
            decimal productAmount = Orders.SumOrderProductAmount(orderProductList);
            //满减折扣
            decimal fullCutDiscount = Orders.SumFullCut(orderProductList);
            //订单合计
            decimal orderAmount = productAmount - fullCutDiscount;
            //整理的订单商品列表
            List<OrderProductInfo> tidiedOrderProductList = Orders.TidyOrderProductList(orderProductList);

            CartModel model = new CartModel
            {
                TotalCount = totalCount,
                ProductAmount = productAmount,
                FullCutDiscount = fullCutDiscount,
                OrderAmount = orderAmount,
                OrderProductList = tidiedOrderProductList
            };

            return View(model);
        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        public ActionResult Clear()
        {
            Orders.ClearShopCart(WorkContext.Uid, WorkContext.Sid);
            //将购物车中商品数量写入cookie
            Orders.SetShopCartProductCountCookie(0);

            return Content("success");
        }
    }
}