package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.OrderNumberUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IMicroappService;
import com.lightinit.hsdataportal.service.IMyDataService;
import com.lightinit.hsdataportal.service.IResourceCatelogService;
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.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.Date;
import java.util.List;

@RequestMapping(value = "/share/microapp")
@Controller
public class MicroappShareController {
    @Autowired
    private IMicroappService microappService;
    @Autowired
    private IResourceCatelogService resourceCatelogService ;
    @Autowired
    private IMyDataService myDataService;
    @RequestMapping(value = "shareList.action")
    public ModelAndView shareList(MicroappSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("/share/microapp/index");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        inputModel.setPublishState(DicPublishApiState.PUBLISH);
        inputModel.setSortCategory(DicSortCategory.DEFAULT_SORT);
        List<MicroappListModel> outputModel = microappService.selectShareList(inputModel);
        pager.setTotal(microappService.selectShareCount(inputModel));
        modelAndView.addObject("themes",resourceCatelogService.getResourceCatelogNameByType(DicResourceCatelogType.THEME,DicResourceType.MICROAPP,DicPlatformType.SHARE)) ;
        modelAndView.addObject("sectors",resourceCatelogService.getSectorNameList()) ;
        modelAndView.addObject("chargeMethods",DicPublishSettingChargeMethod.getInstacne().getDicMap()) ;
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("searchModel", inputModel);
        modelAndView.addObject("flag", inputModel.getFlag());
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("imageContextPath", imageContextPath);
        return modelAndView;
    }
    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(HttpSession session,@PathVariable long id) {
        ModelAndView modelAndView = new ModelAndView("/share/microapp/detail");
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        String platformType = DicPlatformType.SHARE;
        MicroappViewModel microappModel = microappService.queryView(id, platformType);
        modelAndView.addObject("microapp", microappModel);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("imageContextPath", imageContextPath);
        List<ResourceAuth> list =null;
        if(loginUser!=null) {
            list =  microappService.isAPPly(loginUser.getId(),microappModel.getPublishSettingId());

        }
        modelAndView.addObject("list",list);
        return modelAndView;
    }
    @RequestMapping(value = "apply.action")
    @ResponseBody
    @RequiresAuthentication
    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.SHARE);
        //判断是否已经申请过
        Long resourceAuthId= myDataService.getResourceAuthId(inputModel.getPublishSettingId(),loginUser.getId(), DicPlatformType.SHARE);
        if(resourceAuthId !=null){
            ResultState resultState = new ResultState();
            resultState.setMsg("已经申请过了，请前往用户中心下载");
            return resultState;
        }
        return microappService.doApply(inputModel);
    }

    /**
     *查询演示
     * resource_microapp表id
     * @param id
     * @return
     */
    @RequestMapping(value = "demo.action/{id}")
    public ModelAndView demo(@PathVariable long id) {
        ModelAndView modelAndView = new ModelAndView("");
        ResourceMicroapp outputModel=microappService.demo(id);
        modelAndView.addObject("microapp",outputModel );
        return modelAndView;
    }

     /*关注
     * setting表id
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value = "follow.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState follow(HttpSession session,@PathVariable long id) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResourceFollow model = new ResourceFollow();
        model.setFollowerId(loginUser.getId());//关注人id
        model.setPublishSettingId(id);//资源发布id
        model.setPlatformType(DicPlatformType.SHARE);
        ResultState   resultState = microappService.follow(model);
        return resultState;
    }
    /**
     *取消关注
     * 关注表id
     * @param
     * @param id
     * @return
     */
    @RequestMapping(value = "delfollow.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState delfollow(@PathVariable long id) {
        ResultState   resultState = microappService.delfollow(id);
        return resultState;
    }

}
