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.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(@RequestBody Auth auth) throws AdminControllerException {
		try { 
			this.authService.remove(auth.getAuthId());
		}
		catch(final MPlusServiceException ex) { 
			this.logger.error("권한 삭제실패. authId : " + auth.getAuthId());
			throw new AdminControllerException(ex); 
		}
	}

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