/*******************************************************************************
 * Copyright 2014 2Fi Business Solutions Ltd.
 *
 * This code is part of the PCS project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.user.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ffii.core.utils.CriteriaUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.user.service.SysGroupService;
import com.ffii.tbms.mail.MailUtils;
import com.ffii.tbms.user.service.AuthService;

@Controller
@RequestMapping(value = "/user/sys-group")
public class SysGroupController extends AbstractController {

	@Autowired
	private SysGroupService sysGroupService;

	@Autowired
	private AuthService authService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Model model, HttpServletRequest request, @RequestParam(defaultValue = "-1") Integer id) throws Exception {

		if(id<=0){
			Map<String, Object> args = new HashMap<>();
			CriteriaUtils.addString(request, args, "name");
			CriteriaUtils.addString(request, args, "description");
			CriteriaUtils.addString(request, args, "companyName");
			CriteriaUtils.addString(request, args, "phone");
			CriteriaUtils.addString(request, args, "fax");
			CriteriaUtils.addString(request, args, "email");
			CriteriaUtils.addString(request, args, "address");
			args.put("mailSubject", MailUtils.TEMP_SUBJECT);
			args.put("mailBody", MailUtils.TEMP_BODY);
			id = sysGroupService.insert(args);
		}else{
			Map<String, Object> args = new HashMap<>();
			args.put("id", id);
			CriteriaUtils.addString(request, args, "name");
			CriteriaUtils.addString(request, args, "description");
			CriteriaUtils.addString(request, args, "companyName");
			CriteriaUtils.addString(request, args, "phone");
			CriteriaUtils.addString(request, args, "fax");
			CriteriaUtils.addString(request, args, "email");
			CriteriaUtils.addString(request, args, "address");
			if(!args.containsKey("description")){
				args.put("description", null);
			}
			sysGroupService.update(args);
		}

		model.addAttribute(Params.ID, id);
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request, @RequestParam Integer id) throws Exception {

			Map<String, Object> args = new HashMap();
			args.put("id", id);
			args.put("deleted", Boolean.TRUE);
			sysGroupService.update(args);
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}



	@RequestMapping(value = "/load", method = { RequestMethod.GET, RequestMethod.POST })
	public String load(Model model, HttpServletRequest request, @RequestParam Integer id) throws Exception {

		Map<String, Object> data = sysGroupService.find(id);

		model.addAttribute(Params.DATA, data);
		model.addAttribute(Params.SUCCESS, data != null);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/combo.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String comboJson(Model model, HttpServletRequest request) throws Exception {

		Map<String, Object> args = new HashMap<String, Object>();

		CriteriaUtils.addInteger(request, args, Params.ID);
		CriteriaUtils.addStringLike(request, args, Params.QUERY);

		List<Map<String, Object>> records = sysGroupService.search(args);

		model.addAttribute(Params.RECORDS, records);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/list.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String listJson(Model model, HttpServletRequest request) throws Exception {

		Map<String, Object> args = new HashMap<String, Object>();

		CriteriaUtils.addInteger(request, args, Params.ID);
		CriteriaUtils.addStringLike(request, args, Params.NAME);
		CriteriaUtils.addInteger(request, args, "userId");
		

		List<Map<String, Object>> records = sysGroupService.search(args);

		model.addAttribute(Params.RECORDS, records);

		return JsonView.NAME;
	}


	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/smtp/save", method = RequestMethod.POST)
	public String saveSMTP(Model model, HttpServletRequest request) throws Exception {
		int id = SecurityUtils.getUser().getSysGroupId();
		if(id>0){
			Map<String, Object> args = new HashMap<>();
			args.put("id", id);
			CriteriaUtils.addString(request, args, "smtp_host");
			CriteriaUtils.addString(request, args, "smtp_port");
			CriteriaUtils.addString(request, args, "smtp_username");
			CriteriaUtils.addString(request, args, "smtp_password");
			sysGroupService.update(args);
		}
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/smtp/load", method = { RequestMethod.GET, RequestMethod.POST })
	public String loadSMTP(Model model, HttpServletRequest request) throws Exception {

		Map<String, Object> sysGroup = sysGroupService.find(SecurityUtils.getUser().getSysGroupId());

		Map<String, Object> data = null;
		if(sysGroup != null){
			data = new HashMap<>();
			data.put("smtp_host", sysGroup.get("smtp_host"));
			data.put("smtp_port", sysGroup.get("smtp_port"));
			data.put("smtp_username", sysGroup.get("smtp_username"));
			data.put("smtp_password", sysGroup.get("smtp_password"));
		}

		model.addAttribute(Params.DATA, data);
		model.addAttribute(Params.SUCCESS, data != null);

		return JsonView.NAME;
	}

}
