package com.dbs.mplus.controller.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseBody;

import com.dbs.mplus.exception.admin.AdminControllerException;
import com.dbs.mplus.framework.api.executor.ApiExecuteDelegator;
import com.dbs.mplus.framework.exception.MPlusServiceException;
import com.dbs.mplus.framework.service.admin.auth.AuthService;
import com.dbs.mplus.framework.service.admin.auth.repository.dao.Auth;
import com.dbs.mplus.framework.service.admin.auth.repository.dao.AuthAttr;
import com.dbs.mplus.service.MPlusConfigureService;

@Controller
@RequestMapping("/admin/auth")
public class AuthController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	@Qualifier("apiExecuteDelegator")
    private ApiExecuteDelegator executor;
	@Autowired 
	private MPlusConfigureService configureService; 
	@Autowired
	private AuthService authService;

	@RequestMapping(value = "/view.page", method = RequestMethod.GET)
	public Model view(Model model) throws AdminControllerException {
		try { 
			model.addAttribute("defaultLanguage", this.configureService.getI18nDefault());
			model.addAttribute("supportedLanguages", this.configureService.getI18nSupported().split(","));
		}
		catch(final Exception ex) {
			this.logger.error("", ex);
			throw new AdminControllerException(ex); 
		}
		
		return model;
	}

	@RequestMapping(value = "/getList.json", method = RequestMethod.POST)
	@ResponseBody
	public List<Auth> getAuth(Auth auth) throws AdminControllerException {
		try { 
			return this.authService.getList();
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("추가 속성 가져오기 실패", ex);
			throw new AdminControllerException(ex); 
		}
	}

	@RequestMapping(value = "/save.json", method = RequestMethod.POST)
	@ResponseBody
	public Auth save(@RequestBody Auth auth) throws AdminControllerException {
		try { 
			this.authService.save(auth);
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("권한 추가 실패. auth : " + auth, ex);
			throw new AdminControllerException(ex); 
		}
		
		return auth;
	}

	@RequestMapping(value = "/remove.json", method = RequestMethod.POST)
	public void remove(@RequestParam("authId") String authId) throws AdminControllerException {
		try { 
			this.authService.remove(authId);
		}
		catch(final MPlusServiceException ex) { 
			this.logger.error("권한 삭제실패. authId : " + authId);
			throw new AdminControllerException(ex); 
		}
	}

	/**
     * 파라미터 조회
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getAuthAttrList.json", method = RequestMethod.GET)
    @ResponseBody
    public List<AuthAttr> getAuthAttrList(@RequestParam(value = "authId", required = false) String authId,
    											@RequestParam(value = "authType", required = false) String authType) throws AdminControllerException {
    	
    	try { 
    		return this.authService.getAuthAttrListByAuthId(authId, authType);
    	}
    	catch(final Exception ex) {
    		this.logger.error("권한 조회 실패. authId : " + authId, ex);
    		throw new AdminControllerException(ex); 
    	}
    }
}
