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.DicRoadShowStage;
import com.lightinit.hsdataportal.dictionary.DicRoadShowTrade;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.model.RoadShowSearchModel;
import com.lightinit.hsdataportal.model.RoadshowAddModel;
import com.lightinit.hsdataportal.service.IBusinessIncubationService;
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("/open/roadShow")
public class BusinessIncubationController {
    @Autowired
    private IBusinessIncubationService iBusinessIncubationService;

    @RequestMapping("/index.htm")
    public ModelAndView index(RoadShowSearchModel roadShowSearchModel,@RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("open/roadShow/roadShowlist");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(6);
        pager.setPageNo(pageNo);
        roadShowSearchModel.setPage(pager);
        List<RoadshowWithBLOBs> list = iBusinessIncubationService.getList(roadShowSearchModel);
        pager.setTotal(iBusinessIncubationService.getCount(roadShowSearchModel));
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        modelAndView.addObject("searchModel", roadShowSearchModel);
        modelAndView.addObject("roadShowStage", DicRoadShowStage.getInstacne().getDicMap());
        modelAndView.addObject("roadShowTrade", DicRoadShowTrade.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(RoadShowSearchModel roadShowSearchModel,@RequestParam(defaultValue = "1") int pageNo) {
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(6);
        pager.setPageNo(pageNo);
        roadShowSearchModel.setPage(pager);
        ModelAndView modelAndView = new ModelAndView("open/roadShow/more");
        List<RoadshowWithBLOBs> list =  iBusinessIncubationService.getList(roadShowSearchModel);
        modelAndView.addObject("list", list);
        return modelAndView;
    }
    @RequestMapping(value = "/add.htm")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("open/roadShow/addRoadShow");
        modelAndView.addObject("roadShowStage", DicRoadShowStage.getInstacne().getDicMap());
        modelAndView.addObject("roadShowTrade", DicRoadShowTrade.getInstacne().getDicMap());
        return modelAndView;
    }
    @RequestMapping(value = "/toAdd.action")
    @ResponseBody
    public ResultState toAdd(@Valid RoadshowAddModel roadshowAddModel, BindingResult bindingResult, MultipartFile fileImg, MultipartFile file1, MultipartFile file2, MultipartFile file3, MultipartFile file4) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(roadshowAddModel, bindingResult);
        }
        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)){
                roadshowAddModel.setThumbnail(dataFileDir + fileImg.getOriginalFilename());
            }else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        String uniqueDir1 = GuidGenerator.generate() + File.separator;
        String dataFileDir1 = UploadFileUtils.buildRelativeDir(SysContants.UploadFilePath.ROADSHOW_ROOT_DIR, uniqueDir, SysContants.UploadFilePath.ACTIVITY_IMG_DIR);
        if (file1 != null && !file1.isEmpty()) {
            if (UploadFileUtils.writeUploadFile(file1, dataFileDir1, SysContants.PropertyKey.UPLOAD_IMAGE_ROOT_DIR)) {
                roadshowAddModel.setMaterial1(dataFileDir1 + file1.getOriginalFilename());
            } else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        if (file2 != null && !file2.isEmpty()) {
            if (UploadFileUtils.writeUploadFile(file2, dataFileDir1, SysContants.PropertyKey.UPLOAD_IMAGE_ROOT_DIR)) {
                roadshowAddModel.setMaterial2(dataFileDir1 + file2.getOriginalFilename());
            } else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        if (file3 != null && !file3.isEmpty()) {
            if (UploadFileUtils.writeUploadFile(file3, dataFileDir, SysContants.PropertyKey.UPLOAD_IMAGE_ROOT_DIR)) {
                roadshowAddModel.setMaterial3(dataFileDir1 + file3.getOriginalFilename());
            } else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        if (file4 != null && !file4.isEmpty()) {
            if (UploadFileUtils.writeUploadFile(file4, dataFileDir1, SysContants.PropertyKey.UPLOAD_IMAGE_ROOT_DIR)) {
                roadshowAddModel.setMaterial4(dataFileDir1 + file4.getOriginalFilename());
            } else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        ResultState resultState =  iBusinessIncubationService.toAdd(roadshowAddModel);
        return resultState;
    }
    @RequestMapping("/view.htm/{id}")
    public ModelAndView view(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("open/roadShow/view");
        RoadshowWithBLOBs roadshow = iBusinessIncubationService.view(id);
        modelAndView.addObject("model",roadshow);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("imageContextPath", imageContextPath);
        return modelAndView;
    }
    @RequestMapping("/toLxwm.action")
    @ResponseBody
    public ResultState toLxwm( @Valid RoadshowRequirementWithBLOBs roadshowRequirement, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(roadshowRequirement, bindingResult);
        }
        ResultState resultState = iBusinessIncubationService.toLxwm(roadshowRequirement);
        return resultState;
    }

}
