package com.lightinit.hsdataplatform.controller.admin;

import com.google.code.kaptcha.Constants;
import com.lightinit.hsdataplatform.common.MenuUtils;
import com.lightinit.hsdataplatform.common.RoleModulesUtilsPro;
import com.lightinit.hsdataplatform.dictionary.DicOfflineConferenceOrderState;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.json.pojo.JsonResult;
import com.lightinit.hsdataplatform.json.pojo.ListResult;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.OfflineConferenceListModel;
import com.lightinit.hsdataplatform.model.admin.UserAddModel;
import com.lightinit.hsdataplatform.model.admin.UserAdminSearchModel;
import com.lightinit.hsdataplatform.model.admin.UserEditModel;
import com.lightinit.hsdataplatform.service.IAdminService;
import com.lightinit.hsdataplatform.service.IMenuService;
import com.lightinit.hsdataplatform.service.IRoleService;
import com.lightinit.hsdataplatform.service.IUserModuleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by Mr.Yao on 2017/4/11.
 */
@Controller
@RequestMapping("/admin/user")
public class UserAdminController extends BaseController{
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoleModulesUtilsPro roleModulesUtilsPro;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserModuleService userModuleService;

    @RequestMapping(value = "view/password.action")
    public String print_user_modify_password() {
        return "user/modifypwd";
    }

    @RequestMapping(value = "/index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/user/index");
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(UserAdminSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/user/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Admin> outputModel = adminService.queryList(inputModel, pager);
        pager.setTotal(adminService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }


    @RequestMapping(value = "add.htm")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("/admin/user/add");
        return modelAndView;
    }

    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState add_action(@Valid UserAddModel userAddModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(userAddModel, bindingResult);
        }
        return adminService.addAdmin(userAddModel);
    }

    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/admin/user/edit");
        modelAndView.addObject("admin", adminService.QueryOne(id));
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState edit_action(@Valid UserEditModel userEditModel,BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(userEditModel,bindingResult) ;
        }
        return adminService.editAdmin(userEditModel);
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState<Long> delete_action(@PathVariable Long id) {
        return adminService.deleteAdmin(id);
    }

    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/admin/user/view");
        modelAndView.addObject("admin", adminService.getAdminViewModel(id));
        return modelAndView;
    }

    @RequestMapping(value = "add_role.htm/{id}")
    public ModelAndView addRole(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/user/add_role");
        modelAndView.addObject("busiModel", adminService.QueryOne(id));
        AdminRole adminRole = null;
        List<AdminRole> adminRolesList = roleService.QueryRoleList(id);
        if (adminRolesList != null && adminRolesList.size() > 0){
            adminRole = adminRolesList.get(0);
        }
        modelAndView.addObject("adminRole", adminRole);
        return modelAndView;
    }

    @RequestMapping(value = "add_role.action")
    @ResponseBody
    public ResultState doAddRole(@RequestParam Long id, @RequestParam Long roleId){
        if (id == null || roleId == null){
            return buildInvalidResultState("数据不正确");
        }
        List<Long> roleIdList = new ArrayList<>();
        roleIdList.add(roleId);
        boolean isSuccess = roleService.SaveUserRoles(id, roleIdList);
        if (!isSuccess){
            return buildInvalidResultState("保存角色失败");
        }
        return new ResultState();
    }
}