package com.ffii.tbms.setting.web;

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

import javax.servlet.http.HttpServletRequest;

import com.ffii.core.Settings;
import com.ffii.core.setting.service.SettingsService;
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.order.service.InvoiceService;

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;

@Controller
@RequestMapping(value = "/setting")
public class SettingsController extends AbstractController {

	@Autowired
	private SettingsService settingsService;

	@Autowired
	private InvoiceService invoiceService;


	@RequestMapping(value = "/mail/save", method = { RequestMethod.GET, RequestMethod.POST })
	public String saveMail(Model model, HttpServletRequest request, @RequestParam String subject, @RequestParam String body) throws Exception {

		if (SecurityUtils.isGranted("MATERIAL_MAIL_TEMPLATE")) {

			settingsService.resetSettingsCache();
			invoiceService.updateTemplateBody(body);
			invoiceService.updateTemplateSubject(subject);

			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}

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

		if (SecurityUtils.isGranted("MATERIAL_MAIL_TEMPLATE")) {
			Map<String, Object> map = new HashMap<>();

			map.put("subject", invoiceService.getTemplateSubject());
			map.put("body", invoiceService.getTemplateBody());

			model.addAttribute(Params.DATA, map);
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}

	@RequestMapping(value = "/cache-clear", method = { RequestMethod.GET, RequestMethod.POST })
	public String cacheClear(Model model, HttpServletRequest request) throws Exception {

		if (SecurityUtils.isGranted("SUPERUSER")) {
			settingsService.resetSettingsCache();
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}

	@RequestMapping(value = "/sys-user/details/save", method = { RequestMethod.GET, RequestMethod.POST })
	public String saveSysUserDetails(Model model, HttpServletRequest request) throws Exception {

		if (SecurityUtils.isGranted("MATERIAL_MAIL_TEMPLATE")) {
			Map<String, Object> map = new HashMap<>();
			
			map.put("subject", invoiceService.getTemplateSubject());
			map.put("body", invoiceService.getTemplateBody());

			model.addAttribute(Params.DATA, map);
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}

	@RequestMapping(value = "/sys-user/details/load", method = { RequestMethod.GET, RequestMethod.POST })
	public String loadSysUserDetails(Model model, HttpServletRequest request) throws Exception {

		if (SecurityUtils.isGranted("MATERIAL_MAIL_TEMPLATE")) {
			Map<String, Object> map = new HashMap<>();
			
			map.put("subject", invoiceService.getTemplateSubject());
			map.put("body", invoiceService.getTemplateBody());

			model.addAttribute(Params.DATA, map);
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}


}
