package com.caimei.www.controller.unlimited;

import com.caimei.www.controller.BaseController;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.page.ImageLink;
import com.caimei.www.pojo.page.PageContent;
import com.caimei.www.pojo.page.PageFloor;
import com.caimei.www.service.page.SinglePageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 二级页面
 *
 * @author : Charles
 * @date : 2020/7/20
 */
@Controller
public class SinglePageController extends BaseController {
    /** 找产品/找仪器/找项目 */
	private static final String TOPIC_PATH = "single-page/topic";
	/** 专题活动列表页 */
	private static final String PROMOTIONS_PATH = "single-page/promotions";
	/** 自由页面 */
	private static final String FREE_PAGE_PATH = "single-page/page";
	/** 采美直播页面 */
	private static final String LIVE_PATH = "single-page/live";
	/** 品牌招商介绍页 */
	private static final String INVESTMENT_PATH = "single-page/investment";
	/** 维修保养介绍页 */
	private static final String REPAIR_PATH = "single-page/repair";

    private SinglePageService singlePageService;
    @Autowired
    public void setSinglePageService(SinglePageService singlePageService) {
        this.singlePageService = singlePageService;
    }

    /**
     * 二级页面（找产品/找仪器/找项目/正品联盟）
     */
    @GetMapping("/topic.html")
    public String topic(final Model model, @RequestParam("type") Integer id) {
        PageContent topicPage = singlePageService.getTopicPageById(id);
        if (topicPage == null){
            return super.errorPath();
        }
        model.addAttribute("pageData", topicPage);
        return TOPIC_PATH;
    }

    /**
     * 专题活动列表页
     */
    @GetMapping("/promotions.html")
    public String promotions() {
        return PROMOTIONS_PATH;
    }

    /**
     * 自由页面
     */
    @GetMapping("/page.html")
    public String freePage(final Model model, Integer id) {
        PageContent freePage = singlePageService.getFreePageById(id);
        if (freePage == null){
            return super.errorPath();
        }
        model.addAttribute("pageData", freePage);
        return FREE_PAGE_PATH;
    }

    /**
     * 采美直播页面
     */
    @GetMapping("/live.html")
    public String livePage(final Model model) {
        PageContent livePage = singlePageService.getLivePageData();
        if (livePage == null){
            return super.errorPath();
        }
        model.addAttribute("pageData", livePage);
        return LIVE_PATH;
    }

    /**
     * 品牌招商介绍页
     */
    @GetMapping("/investment.html")
    public String investment(final Model model) {
        return INVESTMENT_PATH;
    }

    /**
     * 维修保养介绍页
     */
    @GetMapping("/repair.html")
    public String repair(final Model model) {
        return REPAIR_PATH;
    }

    /**
     * 二级专题数据
     * @return
     */
    @GetMapping("/page/topic")
    @ResponseBody
    public JsonModel<List<PageFloor>> getTopicDataById(@RequestParam("type") Integer id) {
        return singlePageService.getTopicDataById(id);
    }

    /**
     * 专题活动列表数据
     * @return
     */
    @GetMapping("/promotions/list")
    @ResponseBody
    public JsonModel<PageInfo<ImageLink>> gePromotionsList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return singlePageService.gePromotionsList(pageNum, pageSize);
    }

    /**
     * 品牌招商表格下载
     */
    @GetMapping("/investment/export")
    @ResponseBody
    public Mono<Void> downloadByWriteWith(ServerHttpResponse response) throws UnsupportedEncodingException, FileNotFoundException {
        return singlePageService.downloadByWriteWith(response);
    }

    /**
     * 获取讲师列表
     */
    @GetMapping("/live/teacher")
    @ResponseBody
    public JsonModel<List<ImageLink>> getLiveTeachers() {
        return singlePageService.getLiveTeachers();
    }


}
