package com.dbs.mplus.controller.admin;

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

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.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.AttrService;
import com.dbs.mplus.framework.service.admin.attr.AttrTypeService;
import com.dbs.mplus.framework.service.admin.attr.ExtraAttrService;
import com.dbs.mplus.framework.service.admin.attr.repository.dao.Attr;
import com.dbs.mplus.framework.service.admin.attr.repository.dao.AttrType;
import com.dbs.mplus.service.MPlusConfigureService;

/**
 * 속성 관리 Controller (권한 속성, 추가 속성 관리, 사용자 속성등에서 사용될수 있다.)
 * @author heesungkim
 *
 */
@Controller
@RequestMapping("/admin/attr")
public class AttrController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	@Qualifier("apiExecuteDelegator")
    private ApiExecuteDelegator executor;
	@Autowired 
	private MPlusConfigureService configureService; 
	@Autowired
	private AttrService attrService;
	@Autowired
	private AttrTypeService attrTypeService;
	@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 = "/getAttrTypeList.json", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object> getAttrTypeList(AttrType attrType) throws AdminControllerException {
		
		try { 
			Map<String, Object> resultMap = new HashMap<String, Object>();
			
			// 속성 타입 가져오기 MP_ATTR_TYPE
			List<AttrType> attrTypeList = this.attrTypeService.getList();
			
			// 각 타입별로 속성 가져오기 MP_ATTR
			for (AttrType attrType2 : attrTypeList) {
				// 추가 속성과 구분해서 가져온다 (MP_EXTRA_ATTR)
				if(attrType2.getAttrType().equals("extra")) {
					resultMap.put(attrType2.getAttrType(), this.extraAttrService.getList());
				} else {
					resultMap.put(attrType2.getAttrType(), this.attrService.findListByAttrType(attrType2.getAttrType()));
					// 속성 타입이 duty이면
					// duty 타입의 속성이 종속된 work 타입을 가졌을 가능성에 대한 고려?
					// 다른 종속관계가 발생할 가능성은?
					if(attrType2.getAttrType().equals("duty")) {
						List<Attr> attrList = this.attrService.findListByAttrType(attrType2.getAttrType());
						for (Attr attr : attrList) {
							List<Attr> attrList2 = this.attrService.findListByUpperAttr(attr.getAttrType(), attr.getAttrId());
							resultMap.put(attr.getAttrId(), attrList2);
						}
					}
				}
			}
			
			resultMap.put("attrType", attrTypeList);
			
			return resultMap;
			
		} catch(final MPlusServiceException ex) {
			this.logger.error("추가 속성 가져오기 실패", ex);
			throw new AdminControllerException(ex); 
		}
	}
	
}
