package com.xingxi.api.controller;

import com.xingxi.api.common.BaseApiController;
import com.xingxi.api.model.*;
import com.xingxi.api.service.ProdServiceI;
import com.xingxi.business.Banner.domain.Banner;
import com.xingxi.business.Banner.service.IBannerService;
import com.xingxi.business.NewProd.domain.NewProd;
import com.xingxi.business.NewProd.service.INewProdService;
import com.xingxi.business.PopularProd.domain.PopularProd;
import com.xingxi.business.PopularProd.service.IPopularProdService;
import com.xingxi.business.SuggestProd.domain.SuggestProd;
import com.xingxi.business.SuggestProd.service.ISuggestProdService;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.master.ipInfo.domain.IpInfo;
import com.xingxi.master.ipInfo.service.IIpInfoService;
import com.xingxi.master.product.domain.ProdClass;
import com.xingxi.master.product.domain.ProdPic;
import com.xingxi.master.product.service.IProdClassService;
import com.xingxi.master.product.service.IProdPicService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/store/homepage")
public class HomepageApiController extends BaseApiController {

    private final IBannerService bannerService;
    private final IIpInfoService ipInfoService;
    private final IProdClassService prodClassService;
    private final IPopularProdService leftProdService;
    private final ISuggestProdService middleProdService;
    private final INewProdService rightProdService;
    private final IProdPicService prodPicService;
    private final ProdServiceI prodService;

    @GetMapping("/banners")
    public BannerResponse getBanners(){
        Banner cond = new Banner();
        cond.setDelFlag(EDelFlag.NO.getVal());

        return new BannerResponse(bannerService.selectBannerList(cond));
    }

    @GetMapping("/ipInfos")
    public IpInfoResponse getIpInfos(){

        IpInfo cond = new IpInfo();
        cond.setDelFlag(EDelFlag.NO.getVal());

        return new IpInfoResponse(ipInfoService.selectIpInfoList(cond));
    }

    @GetMapping("/titles")
    public SysConfigResponse getTitles(){

        List<String> rtn = new ArrayList<>();

//        // TODO title的key待确认，数据库中未创建数据
        rtn.add(prodService.selectConfigByKey("title.left"));
        rtn.add(prodService.selectConfigByKey("title.middle"));
        rtn.add(prodService.selectConfigByKey("title.right"));

        return new SysConfigResponse(rtn);
    }

    @GetMapping("/leftProds")
    public LeftProdListResponse getLeftProds(){

        PopularProd cond = new PopularProd();
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<PopularProd> leftprodList = leftProdService.selectPopularProdList(cond);

        List<HomeProdResponse> prodList = new ArrayList<>();

        for (PopularProd leftProd : leftprodList) {
            HomeProdResponse prodResponse = new HomeProdResponse();
            prodResponse.setId(leftProd.getId());
            prodResponse.setProdId(leftProd.getProdId());
            prodResponse.setSort(leftProd.getSort());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(leftProd.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicService.selectProdPicList(condPic);
            prodResponse.setProdPicList(picList);

            prodList.add(prodResponse);
        }

        return new LeftProdListResponse(prodList);
    }

    @GetMapping("/middleProds")
    public MiddleProdListResponse getMiddleProds(){

        SuggestProd cond = new SuggestProd();
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<SuggestProd> leftprodList = middleProdService.selectSuggestProdList(cond);

        List<HomeProdResponse> prodList = new ArrayList<>();

        for (SuggestProd leftProd : leftprodList) {
            HomeProdResponse prodResponse = new HomeProdResponse();
            prodResponse.setId(leftProd.getId());
            prodResponse.setProdId(leftProd.getProdId());
            prodResponse.setSort(leftProd.getSort());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(leftProd.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicService.selectProdPicList(condPic);
            prodResponse.setProdPicList(picList);

            prodList.add(prodResponse);
        }

        return new MiddleProdListResponse(prodList);
    }

    @GetMapping("/rightProds")
    public RightProdListResponse getRightProds(){

        NewProd cond = new NewProd();
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<NewProd> leftprodList = rightProdService.selectNewProdList(cond);

        List<HomeProdResponse> prodList = new ArrayList<>();

        for (NewProd leftProd : leftprodList) {
            HomeProdResponse prodResponse = new HomeProdResponse();
            prodResponse.setId(leftProd.getId());
            prodResponse.setProdId(leftProd.getProdId());
            prodResponse.setSort(leftProd.getSort());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(leftProd.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicService.selectProdPicList(condPic);
            prodResponse.setProdPicList(picList);

            prodList.add(prodResponse);
        }

        return new RightProdListResponse(prodList);
    }

    @GetMapping("/prodClasses")
    public ProdClassResponse getProdClasses(){

        ProdClass cond = new ProdClass();
        cond.setDelFlag(EDelFlag.NO.getVal());

        return new ProdClassResponse(prodClassService.selectProdClassList(cond));
    }

}
