package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.controller.BaseController;
import com.lightinit.hsdataportal.dictionary.DicActivityState;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicRqrmntSourceType;
import com.lightinit.hsdataportal.dictionary.DicRqrmntType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IAPIDemandService;
import com.lightinit.hsdataportal.service.IMyMicroappRequirementService;
import com.lightinit.hsdataportal.service.IRequirementMicroappService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/open/apiDemand")
public class APIDemandOpenController extends BaseController{

    @Autowired
    private IAPIDemandService demandService ;

    @Autowired
    private IRequirementMicroappService requirementMicroappService;

    @RequestMapping(value = "list.htm")
    public ModelAndView openList(APIDemandOpenSearchModel demandOpenSearchModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("open/apiDemand/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(5);
        pager.setPageNo(pageNo);
        demandOpenSearchModel.setPage(pager);
        demandOpenSearchModel.setPlatformType(DicPlatformType.OPEN);
        demandOpenSearchModel.setRqrmntState(DicActivityState.ONGOING);
        List<APIDemandOpenListModel> outputModel = demandService.queryOpenList(demandOpenSearchModel);
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", demandService.queryOpenCount(demandOpenSearchModel));
        modelAndView.addObject("newTime", new Date());
        modelAndView.addObject("flag",demandOpenSearchModel.getFlag());
        return modelAndView;
    }
    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "add.htm")
    public ModelAndView openList(){
        ModelAndView modelAndView = new ModelAndView("open/apiDemand/add");
        return modelAndView;
    }

    @RequestMapping(value = "release.action")
    @ResponseBody
    public ResultState release(@Valid APIDdmandOpenReleaseModel  releaseModel, BindingResult bindingResult, HttpSession session){
        if(bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(releaseModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return demandService.release(releaseModel,loginUser.getId());
    }


    @RequestMapping(value = "view.action/{id}")
    public ModelAndView openView(@PathVariable Long id, HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        int flag =0 ;
        if (!StringUtils.isEmpty(loginUser)){
            flag = demandService.isFollow(loginUser.getId(),id, DicPlatformType.OPEN) ;
        }
        ModelAndView modelAndView = new ModelAndView("open/apiDemand/view");
        APIDemandOpenViewModel demandOpenViewModel = demandService.queryView(id);
        modelAndView.addObject("demandOpenViewModel", demandOpenViewModel);
        modelAndView.addObject("isFollow", flag);
        modelAndView.addObject("isReceive", demandService.isReceive(id));
        return modelAndView;
    }


    //关注
    @RequestMapping(value = "follow.action")
    @ResponseBody
    public ResultState follow(Long id, HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return demandService.follow(id,loginUser.getId(), DicPlatformType.OPEN);
    }

    /**
     * 接单
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "receive.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState receive(HttpSession session,RequirementReceiveModel inputModel,@PathVariable Long id) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setRqrmntId(id);
        inputModel.setReceivedBy(loginUser.getId());
        ResultState   resultState = requirementMicroappService.receive(inputModel);
        return resultState;
    }

    @RequestMapping(value="getMore.action")
    public ModelAndView getMore(APIDemandOpenSearchModel searchModel,@RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("open/data_requirement/more");
        List<APIDemandOpenListModel> outputModel=demandService.getMore(searchModel,pageNo);
        modelAndView.addObject("outputModel", outputModel);
        return modelAndView;
    }

}
