package com.lightinit.hsdataplatform.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lightinit.hsdataplatform.dictionary.DicModuleActionType;
import com.lightinit.hsdataplatform.entity.Menu;
import com.lightinit.hsdataplatform.entity.ModuleAction;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.model.admin.ModuleDataModel;
import com.lightinit.hsdataplatform.model.admin.TreeModel;
import com.lightinit.hsdataplatform.service.admin.IModuleMgrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author WangYao
 * @date 2018/7/1 14:47
 * @description 模块功能管理控制器
 */
@Controller
@RequestMapping("/admin/module_mgr")
public class ModuleMgrController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ResourceCatelogMgrController.class);

    @Autowired
    private IModuleMgrService moduleMgrService;

    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/module_mgr/index");
        List<Menu> allModuleList = moduleMgrService.getAllMenuList();
        List<ModuleAction> allActionList = moduleMgrService.getAllActionList();
        String allModuleListJsonStr = null;
        String allActionListJsonStr = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            allModuleListJsonStr = objectMapper.writeValueAsString(allModuleList);
            allActionListJsonStr = objectMapper.writeValueAsString(allActionList);
        }catch (JsonProcessingException jpe){
            logger.error(jpe.getMessage());
        }
        modelAndView.addObject("allModuleList", allModuleListJsonStr);
        modelAndView.addObject("allActionList", allActionListJsonStr);
        modelAndView.addObject("dicModuleActionType", DicModuleActionType.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "tree_data.action")
    @ResponseBody
    public List<TreeModel> getTreeData(){
        return moduleMgrService.queryTreeData();
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState doEdit(HttpSession session, @Valid ModuleDataModel inputModel, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setCreatedBy(loginUser.getId());
        ResultState resultState = moduleMgrService.addOrEdit(inputModel);
        return resultState;
    }

    @RequestMapping(value = "delete_module.action/{id}")
    @ResponseBody
    public ResultState deleteDicGroup(@PathVariable long id){
        ResultState resultState = moduleMgrService.deleteModule(id);
        return resultState;
    }

    @RequestMapping(value = "delete_action.action/{id}")
    @ResponseBody
    public ResultState deleteDic(@PathVariable long id){
        ResultState resultState = moduleMgrService.deleteAction(id);
        return resultState;
    }
}
