package com.lightinit.hsdataportal.controller.open;

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.apache.shiro.authz.annotation.RequiresAuthentication;
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.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/open/api")
public class APIOpenController {

    @Autowired
    private IAPIservice apiService ;

    @Autowired
    private IResourceCatelogService resourceCatelogService ;

    @RequestMapping(value = "list.htm")
    public String openList(APIOpenSearchModel apiOpenSearchModel, @RequestParam(defaultValue = "1") int pageNo,Model model){
        apiOpenSearchModel.setNotOpenType(DicResourceOpenType.NOT_OPEN);
        apiOpenSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        apiOpenSearchModel.setPage(pager);
        apiOpenSearchModel.setSortCategory(DicSortCategory.DEFAULT_SORT);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;
        List<APIOpenListModel> outputModel = apiService.queryOpenList(apiOpenSearchModel);
        pager.setTotal(apiService.queryOpenCount(apiOpenSearchModel));
        model.addAttribute("themes",resourceCatelogService.getResourceCatelogNameByType(DicResourceCatelogType.THEME,DicResourceType.API,DicPlatformType.OPEN)) ;
        model.addAttribute("sectors",resourceCatelogService.getSectorNameList()) ;
        model.addAttribute("chargeMethods",DicPublishSettingChargeMethod.getInstacne().getDicMap()) ;
        model.addAttribute("lists", outputModel);
        model.addAttribute("pager", pager);
        List<APIServiceTypeModel> apiServiceTypeModels = apiService.getApiServiceTypes(DicGroupName.FUWUTYPE) ;
        model.addAttribute("dicAPIServiceType", apiServiceTypeModels);
        model.addAttribute("apiCount", apiService.queryOpenCount(apiOpenSearchModel));
        model.addAttribute("searchModel", apiOpenSearchModel);
        return "open/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.OPEN) ;
        }
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;
        APIOpenViewModel outputModel = apiService.queryOpenView(id,settingId,DicPlatformType.OPEN);
        model.addAttribute("apiOpenViewModel", outputModel);
        model.addAttribute("isFollow",flag );
        model.addAttribute("mianfei",DicPublishSettingChargeMethod.FREE) ;
        return "open/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.OPEN);
    }

    @RequestMapping(value = "offlineConference.action")
    @ResponseBody
    public ResultState offlineConference(APIOfflineConferenceModel offlineConferenceModel,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return apiService.offlineConference(offlineConferenceModel,loginUser.getId());
    }

    @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.OPEN,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.OPEN,applyReason);
    }

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