package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IAPIservice;
import com.lightinit.hsdataportal.service.IResourceCatelogService;
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 javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/share/api")
public class APIShareController {

    @Autowired
    private IAPIservice apiService ;

    @Autowired
    private IResourceCatelogService resourceCatelogService ;


    @RequestMapping(value = "list.htm")
    public String openList(APIShareSearchModel apiShareSearchModel, @RequestParam(defaultValue = "1") int pageNo, Model model){
        apiShareSearchModel.setNotShareType(DicResourceShareType.NOT_SHARE);
        apiShareSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        apiShareSearchModel.setPage(pager);
        apiShareSearchModel.setSortCategory(DicSortCategory.DEFAULT_SORT);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;
        List<APIShareListModel> outputModel = apiService.queryShareList(apiShareSearchModel);
        pager.setTotal(apiService.queryShareCount(apiShareSearchModel));
        model.addAttribute("themes",resourceCatelogService.getResourceCatelogNameByType(DicResourceCatelogType.THEME, DicResourceType.API,DicPlatformType.SHARE)) ;
        model.addAttribute("sectors",resourceCatelogService.getSectorNameList()) ;
        model.addAttribute("lists", outputModel);
        model.addAttribute("pager", pager);
        model.addAttribute("apiCount", apiService.queryShareCount(apiShareSearchModel)) ;
        model.addAttribute("searchModel", apiShareSearchModel);
        return "share/api/index";
    }

    @RequestMapping(value = "view.htm")
    public String view( Long id,Long settingId,Model model,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        int flag =0 ;
        if (!StringUtils.isEmpty(loginUser)){
            flag = apiService.isFollow(loginUser.getId(),settingId,DicPlatformType.SHARE) ;
        }
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;
        APIOpenViewModel outputModel = apiService.queryOpenView(id,settingId,DicPlatformType.SHARE);
        model.addAttribute("apiOpenViewModel", outputModel);
        model.addAttribute("mianfei",DicPublishSettingChargeMethod.FREE) ;
        model.addAttribute("isFollow",flag );
        return "share/api/view";
    }

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

    @RequestMapping(value = "apply.action/{id}")
    @ResponseBody
    public ResultState apply(@PathVariable Long id,String applyReason,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return apiService.apply(id,loginUser.getId(),DicPlatformType.SHARE,applyReason);
    }

    @RequestMapping(value = "isApply.action/{id}")
    @ResponseBody
    public boolean isApply(@PathVariable Long id,String applyReason,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return apiService.isApply(id,loginUser.getId(),DicPlatformType.SHARE,applyReason);
    }

}
