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.attr.ExtraAttrService;
import com.dbs.mplus.framework.service.admin.attr.repository.dao.ExtraAttr;
import com.dbs.mplus.framework.service.admin.attr.repository.dao.ExtraAttrUser;
import com.dbs.mplus.framework.service.admin.menu.repository.dao.Menu;
import com.dbs.mplus.service.MPlusConfigureService;



@Controller
@RequestMapping("/admin/extraAttr")
public class ExtraAttrController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	@Qualifier("apiExecuteDelegator")
    private ApiExecuteDelegator executor;
	@Autowired 
	private MPlusConfigureService configureService; 
	@Autowired
	private ExtraAttrService extraAttrService;

	@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<ExtraAttr> getMenuList(Menu menu) throws AdminControllerException {
		try { 
			return this.extraAttrService.getList();
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("추가 속성 가져오기 실패", ex);
			throw new AdminControllerException(ex); 
		}
	}

	@RequestMapping(value = "/save.json", method = RequestMethod.POST)
	@ResponseBody
	public ExtraAttr save(@RequestBody ExtraAttr extraAttr) throws AdminControllerException {
		try { 
			this.extraAttrService.save(extraAttr);
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("추가 속성 저장실패. extraAttr : " + extraAttr, ex);
			throw new AdminControllerException(ex); 
		}
		
		return extraAttr;
	}

	@RequestMapping(value = "/remove.json", method = RequestMethod.POST)
	public void remove(@RequestBody ExtraAttr extraAttr) throws AdminControllerException {
		try { 
			this.extraAttrService.remove(extraAttr.getExtraAttrId());
		}
		catch(final MPlusServiceException ex) { 
			this.logger.error("추가 속성 삭제실패. extraAttrId : " + extraAttr.getExtraAttrId());
			throw new AdminControllerException(ex); 
		}
	}

	/**
     * 파라미터 조회
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getExtraAttrUserList.json", method = RequestMethod.POST)
    @ResponseBody
    public List<ExtraAttrUser> getExtraAttrUserList(@RequestBody ExtraAttr extraAttr) throws AdminControllerException {
    	try { 
    		return this.extraAttrService.getExtraAttrUserListByExtraAttrId(extraAttr.getExtraAttrId());
    	}
    	catch(final Exception ex) {
    		this.logger.error("추가속성 사용자 조회 실패 : " + extraAttr.getExtraAttrId(), ex);
    		throw new AdminControllerException(ex); 
    	}
    }
}
