package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicResourceType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IMicroappService;
import com.lightinit.hsdataportal.service.IMyApiRequirementService;
import com.lightinit.hsdataportal.service.IMyMicroappService;
import com.lightinit.hsdataportal.service.IResourceFollowService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
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 org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/open/myMicroapp")
public class MyMicroappController {
    @Autowired
    private IMyMicroappService myMicroappService;
    @Autowired
    private IResourceFollowService followService ;

    /**
     * 申请列表
     * @param inputModel
     * @param pageNo
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "list.htm")
    public String applyList(@RequestParam(defaultValue = "0")Long falg,Long msg ,MyMicroappApplySearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo,Model model, HttpSession session) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setApplyBy(loginUser.getId());
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        inputModel.setResourceType(DicResourceType.MICROAPP);
        inputModel.setPlatformType(DicPlatformType.OPEN);
        List<MyMicroappApplyListModel> applyListModels = myMicroappService.selectOpenList(inputModel);
        model.addAttribute("applyList", applyListModels);
        MyMicroappFollowSearchModel followSearchModel = new MyMicroappFollowSearchModel() ;
        BeanUtils.copyProperties(inputModel,followSearchModel);
        followSearchModel.setFollowId(loginUser.getId());
        List<MyMicroappFollowListModel> followListModels = myMicroappService.selectFollowOpenList(followSearchModel);
        model.addAttribute("followList", followListModels);
        model.addAttribute("falg", falg);
        model.addAttribute("msg", msg);
        return "open/myMicroapp/list";
    }

    /**
     * 关注列表
     * @param
     * @param
     * @return
     */
//    public ModelAndView followList(MyMicroappFollowSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
//        ModelAndView modelAndView = new ModelAndView("admin/microapp_register/list");
//        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
//        inputModel.setPage(pager);
//        inputModel.setResourceType(DicResourceType.MICROAPP);
//        inputModel.setPlatformType(DicPlatformType.OPEN);
//        List<MyMicroappFollowListModel> outputModel = myMicroappService.selectFollowOpenList(inputModel);
//        pager.setTotal(myMicroappService.selectFollowOpenCount(inputModel));
//        modelAndView.addObject("list", outputModel);
//        modelAndView.addObject("pager", pager);
//        return modelAndView;
//    }

    //查看反馈
    @RequestMapping(value = "feedback.htm/{id}")
    @ResponseBody
    public MyMicroappFeedbackModel feedback(@PathVariable Long id){

        return myMicroappService.feedback(id, DicPlatformType.OPEN);
    }

//    @RequestMapping(value = "apply.action")
//    public ResultState doApply(HttpSession session, MicroappApplyModel inputModel, String chargeMethod, String openCondition ) {
//        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
//        inputModel.setApplier(loginUser.getId());//在申请人
//        inputModel.setPlatformType(DicPlatformType.OPEN);
//        return microappService.doApply(inputModel,openCondition);
//    }

    @RequestMapping(value = "noFollow.action/{id}")
    public String view(@PathVariable Long id){
        ResultState  resultState  =  followService.noFollow(id);
        if (StringUtils.isEmpty(resultState.getMsg())){
            return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/open/myMicroapp/list.htm?falg=1&msg="+resultState.getMsg()+"#shujv" ;
        }
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/open/myMicroapp/list.htm?falg=1#shujv" ;

    }
}
