package com.sooka.module.web.cms;


import com.sooka.common.utils.ControllerUtil;
import com.sooka.common.utils.JsonUtil;
import com.sooka.module.web.cms.service.VoteLogService;
import com.sooka.module.web.cms.service.VoteService;
import com.sooka.module.web.cms.vo.VoteVo;
import com.sooka.mybatis.model.TCmsVote;
import com.sooka.mybatis.model.TCmsVoteLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Date;

@Controller
@RequestMapping("/cms/vote")
public class VoteController {

    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteLogService voteLogService;

    @RequestMapping
    public String index(@RequestParam(value = "pageCurrent",defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize",defaultValue = "100") Integer pageSize,
                        TCmsVote pojo, Model model){
        model.addAttribute("model",voteService.page(pageNumber,pageSize,pojo));
        model.addAttribute("pojo",pojo);
        return "cms/vote_list";
    }

    /* 跳转增加或修改主题页 */
    @RequestMapping("/input")
    public String input(@RequestParam(value = "id",required = false) Integer vote_id,Model model){
        if(vote_id!=null) {
            model.addAttribute("vote",voteService.findById(vote_id));
        }
        return "cms/vote_topic";
    }
    /* 查看投票结果 */
    @RequestMapping("/show")
    public String show(@RequestParam(value = "id",required = false) Integer vote_id,String content, Model model){
        if(vote_id!=null) {
            List<TCmsVote> list = voteService.findOption(vote_id);
            model.addAttribute("content",content);
            model.addAttribute("opList",list);
        }
        return "cms/vote_show";
    }

    /* 保存或修改投票主题 */
    @RequestMapping("/saveTopic")
    @ResponseBody
    public String saveTopic(VoteVo vote){
        if(vote.getVote_id()!=null){
            return voteService.update(vote);
        }
        return voteService.save(vote);
    }

    /* 跳转增加选项页 */
    @RequestMapping("/toOption")
    public String  toOption(Integer vote_id,Model model){
        List<TCmsVote> list = voteService.findOption(vote_id);
        model.addAttribute("opList",list);
        model.addAttribute("xh",voteService.findById(vote_id).getXh());
        model.addAttribute("flag",vote_id);
        return "cms/vote_option";
    }

    /* 保存投票选项 */
    @RequestMapping("/saveOption")
    @ResponseBody
    public String saveOption(String[] vote_option,Integer flag,Integer xh){
        List<String> vote_option1= new ArrayList<>(Arrays.asList(vote_option));
        voteService.deleteOp(flag);
        return voteService.saveOp(vote_option1,flag,xh);
    }

    /* 查看 */
    @RequestMapping("/toView")
    public String toView(Integer vote_id,String content,Model model){
        List<TCmsVote> list = voteService.findOption(vote_id);
        model.addAttribute("content",content);
        model.addAttribute("opList",list);
        return "cms/vote_view";
    }

    /* 删除 */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int vote_id){
        return voteService.delete(vote_id);
    }

    /* 查找投票主题和选项 */
    @RequestMapping("/findTO")
    @ResponseBody
    public List<TCmsVote> findTO(){
        return voteService.findTO();
    }

    /* 投票 */
    @RequestMapping("/vote")
    @ResponseBody
    public String vote(Integer vote_id,HttpServletRequest request) {
        //TODO 传入id为选项id.现有判断为查询数据库ip，未实现功能：反查出标题id存入数据库，通过标题id和ip查出相应数据
        TCmsVoteLog pojo = new TCmsVoteLog();
        //获取客户端IP
        String IP = ControllerUtil.getRemoteAddress(request);
        Date end = new Date();
        //判断IP，有IP记录
        if (voteLogService.checkIp(IP) > 0) {
            pojo = voteLogService.findByIP(IP);
            Date start = pojo.getVotetime();
            long cha = end.getTime() - start.getTime();
            double result = cha * 1.0 / (1000 * 60 * 60);
            if (result >= 24) {
                pojo.setVotetime(end);
                pojo.setVote_id(vote_id);
                voteLogService.update(pojo);
                return voteService.vote(vote_id);
            }
            if (result < 24) {
                if (voteLogService.checkContent(IP, vote_id) > 0) {
                    return JsonUtil.toERROR("明天再来投票吧！");
                } else {
                    pojo.setVotetime(end);
                    pojo.setVote_id(vote_id);
                    voteLogService.update(pojo);
                    return voteService.vote(vote_id);
                }
            }
        }
            //无IP记录
            pojo.setIP(IP);
            pojo.setVotetime(end);
            pojo.setVote_id(vote_id);
            voteLogService.save(pojo);
            return voteService.vote(vote_id);
    }
}
