package com.slodon.b2b2c.controller.cms.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.slodon.b2b2c.controller.goods.front.FrontGoodsCategoryController;
import com.slodon.b2b2c.core.constant.RedisConst;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.vo.cms.FrontArticleCategoryVO;
import com.slodon.b2b2c.vo.goods.FrontGoodsCategoryVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author cyan
 */
@Api(tags = "front-站点地图")
@Slf4j
@RestController
@RequestMapping("v3/cms/front/sitemap")
public class SiteMapController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private FrontArticleController frontArticleController;

    @Resource
    private FrontGoodsCategoryController frontGoodsCategoryController;

    /**
     * 站点地图
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    public JsonResult getSiteMapData(HttpServletRequest request) {
        String dataJson = stringRedisTemplate.opsForValue().get(RedisConst.PC_INDEX);
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(dataJson)) {
            Set<String> getOldProductCategoryKeys = stringRedisTemplate.keys("Site_ProductCategory::*");
            if (!getOldProductCategoryKeys.isEmpty()) {
                for (String oldProductCategoryKey : getOldProductCategoryKeys) {
                    stringRedisTemplate.delete(oldProductCategoryKey);
                }
            }
            JSONArray jsonArray = JSONObject.parseArray(dataJson);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                log.info("jsonObject:{}", JSON.toJSONString(jsonObject));
                JSONObject hotProductJson = jsonObject.getJSONObject("json");
                if (hotProductJson.containsKey("title_info")) {
                    JSONObject hotProductTitleInfoObject = hotProductJson.getJSONObject("title_info");
                    if (hotProductTitleInfoObject.containsKey("title_name")) {
                        if ("Hot Products".equals(hotProductTitleInfoObject.getString("title_name"))) {
                            JSONObject hotProductsData = hotProductJson.getJSONObject("data");
                            List<Map<String, Object>> goodsNameMap = new ArrayList<>();
                            JSONArray goodsDataArray = hotProductsData.getJSONArray("goods_data");
                            for (int j = 0; j < goodsDataArray.size(); j++) {
                                JSONObject goodsDataObject = goodsDataArray.getJSONObject(j);
                                Map<String, Object> map1 = new HashMap<>();
                                map1.put("goodsName", goodsDataObject.getString("goodsName"));
                                map1.put("productId", goodsDataObject.getString("productId"));
                                goodsNameMap.add(map1);
                            }
                            map.put("hotProducts", goodsNameMap);
                        }

                        if ("Recommended Shop".equals(hotProductTitleInfoObject.getString("title_name"))) {
                            JSONObject hotProductsData = hotProductJson.getJSONObject("data");
                            List<Map<String, Object>> storeMap = new ArrayList<>();
                            JSONArray goodsDataArray = hotProductsData.getJSONArray("store_data");
                            for (int j = 0; j < goodsDataArray.size(); j++) {
                                JSONObject goodsDataObject = goodsDataArray.getJSONObject(j);
                                Map<String, Object> map1 = new HashMap<>();
                                map1.put("storeName", goodsDataObject.getString("storeName"));
                                map1.put("storeId", goodsDataObject.getString("storeId"));
                                storeMap.add(map1);
                            }
                            map.put("recommendedShop", storeMap);
                        }

                        if ("Recommending Products".equals(hotProductTitleInfoObject.getString("title_name"))) {
                            JSONObject hotProductsData = hotProductJson.getJSONObject("data");
                            List<Map<String, Object>> recommendProductMap = new ArrayList<>();
                            JSONArray goodsDataArray = hotProductsData.getJSONArray("goods_data");
                            for (int j = 0; j < goodsDataArray.size(); j++) {
                                JSONObject goodsDataObject = goodsDataArray.getJSONObject(j);
                                Map<String, Object> map1 = new HashMap<>();
                                map1.put("goodsName", goodsDataObject.getString("goodsName"));
                                map1.put("productId", goodsDataObject.getString("productId"));
                                recommendProductMap.add(map1);
                            }
                            map.put("recommendingProducts", recommendProductMap);
                        }
                    }
                }

                int goodsCategoryId = jsonObject.getInteger("tplPcId");
                if (goodsCategoryId == 28) {
                    JSONObject hotProductTitleInfoObject = hotProductJson.getJSONObject("title_info");
                    if (hotProductTitleInfoObject.containsKey("title")) {
                        JSONObject goodsCategoryTitleObject = hotProductTitleInfoObject.getJSONObject("title");
                        String categoryTitle = goodsCategoryTitleObject.getString("initialValue");
                        List<Map<String, Object>> goodsMap = new ArrayList<>();
                        JSONObject goodsCategoryInfoObject = hotProductJson.getJSONObject("center");
                        if (goodsCategoryInfoObject.containsKey("data")) {
                            JSONObject goodsCategoryDataInfoObject = goodsCategoryInfoObject.getJSONObject("data");
                            if (goodsCategoryDataInfoObject.containsKey("goods_data")) {
                                JSONArray goodsDataArray = goodsCategoryDataInfoObject.getJSONArray("goods_data");
                                for (int j = 0; j < goodsDataArray.size(); j++) {
                                    Map<String, Object> map2 = new HashMap<>();
                                    JSONObject goodsDataObject = goodsDataArray.getJSONObject(j);
                                    map2.put("goodsName", goodsDataObject.getString("goodsName"));
                                    map2.put("productId", goodsDataObject.getString("productId"));
                                    goodsMap.add(map2);
                                }
                            }
                        }
                        stringRedisTemplate.opsForValue().set("Site_ProductCategory::" + categoryTitle, JSONObject.toJSONString(goodsMap));
                    }
                }
            }
        }

        Set<String> productCategoryKeys = stringRedisTemplate.keys("Site_ProductCategory::*");
        Map<String, Object> productCategoryMapList = new HashMap<>();
        if (!productCategoryKeys.isEmpty()) {
            for (String productCategoryKey : productCategoryKeys) {
                String categoryName = productCategoryKey.replace("Site_ProductCategory::", "");
                String value = stringRedisTemplate.opsForValue().get(productCategoryKey);
                productCategoryMapList.put(categoryName, JSONObject.parseArray(value));
            }
        }
        map.put("productDirectory", productCategoryMapList);

        JsonResult<List<FrontGoodsCategoryVO>> frontGoodsCategoryControllerSecondCategory = frontGoodsCategoryController.getSecondCategory(null);
        List<FrontGoodsCategoryVO> frontGoodsCategoryVOS = frontGoodsCategoryControllerSecondCategory.getData();
        List<Map<String, Object>> productCategorySecondMapList = new ArrayList<>();
        frontGoodsCategoryVOS.forEach(c -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("categoryName", c.getCategoryName());
            map1.put("categoryId", c.getCategoryId());
            productCategorySecondMapList.add(map1);
        });
        map.put("productSecondDirectory", productCategorySecondMapList);

        JsonResult<List<FrontArticleCategoryVO>> frontArticleResult = frontArticleController.helpList(null, 6, 5);
        map.put("helpArticle", frontArticleResult.getData());
        return SldResponse.success(map);
    }

}
