package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.GuidGenerator;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.common.UploadFileUtils;
import com.lightinit.hsdataportal.dictionary.DicActivityState;
import com.lightinit.hsdataportal.dictionary.DicActivityType;
import com.lightinit.hsdataportal.entity.Activity;
import com.lightinit.hsdataportal.entity.ActivityEnroll;
import com.lightinit.hsdataportal.entity.ActivityWithBLOBs;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.ActivityModel;
import com.lightinit.hsdataportal.model.ActivitySearchModel;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.service.IInnovationActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

import static com.lightinit.hsdataportal.controller.BaseController.buildInvalidResultState;
import static com.lightinit.hsdataportal.controller.BaseController.getAllFieldInvalidResultState;

@Controller
@RequestMapping(value = "/open/innovationActivity")
public class InnovationActivityController {
    @Autowired
    private IInnovationActivityService iInnovationActivityService;
    @RequestMapping(value = "/index.htm")
    public ModelAndView index(ActivitySearchModel activitySearchModel,@RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("open/innovationActivity/activitylist");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(6);
        pager.setPageNo(pageNo);
        activitySearchModel.setPage(pager);
      List<ActivityWithBLOBs> list =  iInnovationActivityService.getList(activitySearchModel);
        pager.setTotal(iInnovationActivityService.getCount(activitySearchModel));
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        modelAndView.addObject("searchModel", activitySearchModel);
        modelAndView.addObject("activityType",DicActivityType.getInstacne().getDicMap());
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("imageContextPath", imageContextPath);
        return  modelAndView;
    }
    @RequestMapping(value = "/more.action")
    public ModelAndView more(ActivitySearchModel activitySearchModel,@RequestParam(defaultValue = "1") int pageNo) {
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(6);
        pager.setPageNo(pageNo);
        activitySearchModel.setPage(pager);
        ModelAndView modelAndView = new ModelAndView("open/innovationActivity/more");
        List<ActivityWithBLOBs> list =  iInnovationActivityService.getList(activitySearchModel);
        modelAndView.addObject("list", list);
        return modelAndView;
    }
    @RequestMapping(value = "/add.htm")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("open/innovationActivity/addActivity");
        modelAndView.addObject("activityType",DicActivityType.getInstacne().getDicMap());
        return  modelAndView;
    }
    @RequestMapping(value = "/toAdd.action")
    @ResponseBody
    private ResultState toAdd(@Valid ActivityModel activity, BindingResult bindingResult, MultipartFile fileImg) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(activity, bindingResult);
        }
        if(activity.getFlag().equals("1")) {
            activity.setActivityState(DicActivityState.PENDING);//审核中
        }else{
            activity.setActivityState(DicActivityState.DRAFT);//待提交
        }
        String uniqueDir = GuidGenerator.generate() + File.separator;
        String dataFileDir = UploadFileUtils.buildRelativeDir(SysContants.UploadFilePath.ACTIVITY_ROOT_DIR, uniqueDir, SysContants.UploadFilePath.ACTIVITY_IMG_DIR);
        if (fileImg != null && !fileImg.isEmpty()){
            if (UploadFileUtils.writeUploadFile(fileImg, dataFileDir, SysContants.PropertyKey.UPLOAD_IMAGE_ROOT_DIR)){
                activity.setThumbnail(dataFileDir + fileImg.getOriginalFilename());
            }else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        ResultState resultState = iInnovationActivityService.toAdd(activity);
        return resultState;
    }
    @RequestMapping(value = "/view.htm/{id}")
    public ModelAndView view(@PathVariable Long id) {
        ModelAndView modelAndView =new ModelAndView("open/innovationActivity/activityView");
        ActivityWithBLOBs model = iInnovationActivityService.view(id);
        modelAndView.addObject("model",model);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("imageContextPath", imageContextPath);
        return modelAndView;
    }
    @RequestMapping(value = "/enroll.action")
    @ResponseBody
    public ResultState enroll(@Valid ActivityEnroll activityEnroll, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(activityEnroll, bindingResult);
        }
        ResultState resultState = iInnovationActivityService.enroll(activityEnroll);
        return resultState;
    }
}
