package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IResourceFollowService;
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.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("/share/myApi")
public class MyAPIShareController {

    @Autowired
    private IResourceFollowService followService ;

    //我的Api列表含关注和申请
    @RequiresAuthentication
    @RequestMapping(value = "list.htm")
    public ModelAndView openList(HttpSession session,@RequestParam(defaultValue = "1") int pageNo,@RequestParam(defaultValue = "0")Long falg,Long msg ){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("share/myApi/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<MyAPIOpenFollowListModel> outputModel = followService.queryOpenList(DicPlatformType.SHARE,loginUser.getId(),pager);
        modelAndView.addObject("followList", outputModel);
        List<MyAPIApplyListModel> myAPIApplyListModels = followService.applyList(loginUser.getId(), DicPlatformType.SHARE,pager);
        modelAndView.addObject("applyList", myAPIApplyListModels);
        modelAndView.addObject("falg", falg);
        return modelAndView;
    }

    @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 + "/share/myApi/list.htm?falg=1&msg="+resultState.getMsg()+"#shujv" ;
        }
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/share/myApi/list.htm?falg=1#shujv" ;

    }
    //申请列表
//    @RequestMapping(value = "applyList.action")
//    public ModelAndView applyList(HttpSession session,@RequestParam(defaultValue = "1") int pageNo){
//        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
//        ModelAndView modelAndView = new ModelAndView("share/myApi/applyList");
//        BaseExample.Page pager = new BaseExample.Page();
//        pager.setPageNo(pageNo);
//        List<MyAPIApplyListModel> myAPIApplyListModels = followService.applyList(loginUser.getId(), DicPlatformType.OPEN,pager);
//        pager.setTotal(followService.applyCount(loginUser.getId(), DicPlatformType.SHARE));
//        modelAndView.addObject("list", myAPIApplyListModels);
//        modelAndView.addObject("pager", pager);
//        return modelAndView ;
//    }


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