package cn.com.qmth.stmms.admin.school;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.biz.school.model.School;
import cn.com.qmth.stmms.biz.school.query.SchoolSearchQuery;
import cn.com.qmth.stmms.biz.school.service.SchoolService;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.biz.user.service.UserService;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.enums.UserType;
import cn.com.qmth.stmms.common.utils.Md5EncryptUtils;

@Controller
@RequestMapping("/admin/sys/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private UserService userService;

    @RequestMapping
    public ModelAndView list(HttpServletRequest request, SchoolSearchQuery query) {
        ModelAndView view = new ModelAndView("modules/sys/schoolList");
        query = schoolService.findByQuery(query);
        view.addObject("query", query);
        return view;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addInit(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("modules/sys/schoolEdit");
        view.addObject("school", new School());
        return view;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editInit(HttpServletRequest request, @RequestParam Integer id) {
        School school = schoolService.findById(id);
        if (school != null) {
            ModelAndView view = new ModelAndView("modules/sys/schoolEdit");
            view.addObject("school", school);
            return view;
        } else {
            return new ModelAndView("redirect:/admin/sys/school");
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(HttpServletRequest request, School school) {
        School previous = null;
        if (school.getId() != null) {
            previous = schoolService.findById(school.getId());
        }
        if (previous == null) {
            school.setEnable(true);
            school = schoolService.save(school);
            return "redirect:/admin/sys/school/admin?id=" + school.getId();
        } else {
            previous.setName(school.getName());
            previous.setProvince(school.getProvince());
            previous.setCity(school.getCity());
            previous.setAddress(school.getAddress());
            previous.setDescription(school.getDescription());
            schoolService.save(previous);
        }
        return "redirect:/admin/sys/school";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public ModelAndView adminInit(HttpServletRequest request, @RequestParam Integer id,
            @RequestParam(required = false) String message) {
        School school = schoolService.findById(id);
        if (school != null) {
            User user = userService.findSchoolAdmin(id);
            if (user == null) {
                user = new User();
                user.setSchoolId(id);
            }
            ModelAndView view = new ModelAndView("modules/sys/schoolAdminEdit");
            view.addObject("school", school);
            view.addObject("user", user);
            view.addObject("message", message);
            return view;
        } else {
            return new ModelAndView("redirect:/admin/sys/school");
        }
    }
    
    @RequestMapping(value = "/admin/save", method = RequestMethod.POST)
    public ModelAndView adminSave(HttpServletRequest request, User user) {
        School school = schoolService.findById(user.getSchoolId());
        if (school == null) {
            return new ModelAndView("redirect:/admin/sys/school");
        }
        User previous = userService.findSchoolAdmin(user.getSchoolId());
        String message = "";
        if (previous != null) {
            previous.setLoginName(user.getLoginName());
            previous.setName(user.getName());
            if (StringUtils.isNotBlank(user.getPassword())) {
                previous.setPassword(Md5EncryptUtils.md5(user.getPassword()));
            }
            if (checkLoginName(previous)) {
                userService.save(previous);
                return new ModelAndView("redirect:/admin/sys/school");
            } else {
                message = "登录名重复";
            }
        } else if (StringUtils.isBlank(user.getPassword())) {
            message = "密码不能为空";
        } else if (checkLoginName(user)) {
            user.setStatus(1);
            user.setType(UserType.ADMIN);
            user.setRoleNames(Role.SCHOOL_ADMIN.getName());
            user.setPassword(Md5EncryptUtils.md5(user.getPassword()));
            userService.save(user);
            return new ModelAndView("redirect:/admin/sys/school");
        } else {
            message = "登录名重复";
        }
        return adminInit(request, user.getSchoolId(), message);
    }
    
    @RequestMapping(value = "/viewer", method = RequestMethod.GET)
    public ModelAndView viewerInit(HttpServletRequest request, @RequestParam Integer id,
            @RequestParam(required = false) String message) {
        School school = schoolService.findById(id);
        if (school != null) {
            User user = userService.findSchoolViewer(id);
            if (user == null) {
                user = new User();
                user.setSchoolId(id);
            }
            ModelAndView view = new ModelAndView("modules/sys/schoolViewerEdit");
            view.addObject("school", school);
            view.addObject("user", user);
            view.addObject("message", message);
            return view;
        } else {
            return new ModelAndView("redirect:/admin/sys/school");
        }
    }

    @RequestMapping(value = "/viewer/save", method = RequestMethod.POST)
    public ModelAndView viewerSave(HttpServletRequest request, User user) {
        School school = schoolService.findById(user.getSchoolId());
        if (school == null) {
            return new ModelAndView("redirect:/admin/sys/school");
        }
        User previous = userService.findSchoolViewer(user.getSchoolId());
        String message = "";
        if (previous != null) {
            previous.setLoginName(user.getLoginName());
            previous.setName(user.getName());
            if (StringUtils.isNotBlank(user.getPassword())) {
                previous.setPassword(Md5EncryptUtils.md5(user.getPassword()));
            }
            if (checkLoginName(previous)) {
                userService.save(previous);
                return new ModelAndView("redirect:/admin/sys/school");
            } else {
                message = "登录名重复";
            }
        } else if (StringUtils.isBlank(user.getPassword())) {
            message = "密码不能为空";
        } else if (checkLoginName(user)) {
            user.setStatus(1);
            user.setType(UserType.SCHOOLVIEWER);
            user.setRoleNames(Role.SCHOOL_VIEWER.getName());
            user.setPassword(Md5EncryptUtils.md5(user.getPassword()));
            userService.save(user);
            return new ModelAndView("redirect:/admin/sys/school");
        } else {
            message = "登录名重复";
        }
        return adminInit(request, user.getSchoolId(), message);
    }
    
    private boolean checkLoginName(User user) {
        User previous = userService.findByLoginName(user.getLoginName());
        if (previous == null || (user.getId() != null && previous.getId().equals(user.getId()))) {
            return true;
        } else {
            return false;
        }
    }
}
