package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.game.domain.GameQuestion;
import com.ssj.bean.sys.game.domain.GameQuestionOption;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.service.conch.game.service.GameQuestionOptionService;
import com.ssj.service.conch.game.service.GameQuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author Mount Liu
 * @date 2019/2/21
 */
@Controller
@RequestMapping("/sys/game")
public class GameH5Controller extends BaseController {

	@Autowired
    private GameQuestionService gameQuestionService;

	@Autowired
    private GameQuestionOptionService gameQuestionOptionService;

    @RequestMapping("/questionList")
    public String questionList(Model model,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false, defaultValue = "1") int pageNo)
            throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("LIKE_title", request.getParameter("LIKE_title") == null ? "" : request.getParameter("LIKE_title"));
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<GameQuestion> page = gameQuestionService.findByPage(params, initPage(pageNo, pageSize, Direction.ASC, new String[]{"number"}));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/game/questionList";
    }

    @RequestMapping("/questionAdd")
    public String questionAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        GameQuestion question = gameQuestionService.getById(id);
        model.addAttribute("picture1", "");
        model.addAttribute("picture2", "");
        if (question == null) {
            question = new GameQuestion();
        } else {
            if(StringUtils.isNotBlank(question.getPicture())){
                List<String> pictures = Arrays.asList(question.getPicture());
                for (int i = 0; i < pictures.size(); i++) {
                    model.addAttribute("picture"+(i+1), pictures.get(i));
                }
            }
        }
        model.addAttribute("entity", question);
        return "sys/game/questionAdd";
    }

    @RequestMapping(value = "/questionSave", method = RequestMethod.POST)
    @ResponseBody
    public Response questionSave(GameQuestion question) {
        Response response = new Response();
        if (StringUtils.isBlank(question.getId())) {
            question.setCreateTime(new Date());
        }
        gameQuestionService.save(question);
        response.success();
        return response;
    }

    @RequestMapping(value = "/questionDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response questionDelete(String id) {
        Response response = new Response();
        gameQuestionService.delete(id);
        response.success();
        return response;
    }

    @RequestMapping("/questionOptionList")
    public String questionOptionList(Model model,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false, defaultValue = "1") int pageNo)
            throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("EQ_questionId", "");
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<GameQuestion> page = gameQuestionService.findByPage(params, initPage(pageNo, pageSize));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/game/questionOptionList";
    }

    @RequestMapping("/questionOptionAdd")
    public String questionOptionAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        GameQuestionOption option = gameQuestionOptionService.getById(id);
        if (option == null) {
            option = new GameQuestionOption();
        }
        model.addAttribute("entity", option);
        return "sys/game/questionOptionAdd";
    }

    @RequestMapping(value = "/questionOptionSave", method = RequestMethod.POST)
    @ResponseBody
    public Response questionOptionSave(GameQuestionOption option) {
        Response response = new Response();
        gameQuestionOptionService.save(option);
        if(option.getIsCorrect() == 1){
            GameQuestion question = gameQuestionService.getById(option.getQuestionId());
            question.setAnswerId(option.getQuestionId());
            gameQuestionService.save(question);
        }
        response.success();
        return response;
    }

    @RequestMapping(value = "/questionOptionDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response questionOptionDelete(String id) {
        Response response = new Response();
        gameQuestionOptionService.delete(id);
        response.success();
        return response;
    }
}
