package kr.co.jinwoosi.clfd.conts.site.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import kr.co.jinwoosi.clfd.conts.lang.model.LangSearch;
import kr.co.jinwoosi.clfd.conts.lang.service.LangService;
import kr.co.jinwoosi.clfd.conts.menu.model.Menu;
import kr.co.jinwoosi.clfd.conts.menu.service.MenuService;
import kr.co.jinwoosi.clfd.conts.site.mapper.SiteMapper;
import kr.co.jinwoosi.clfd.conts.site.model.Site;
import kr.co.jinwoosi.clfd.conts.site.model.SiteSearch;
import kr.co.jinwoosi.clfd.conts.site.service.SiteService;
import kr.co.jinwoosi.clfd.framework.base.service.impl.BaseServiceImpl;
import kr.co.jinwoosi.clfd.framework.util.ApplicationHelper;
import kr.co.jinwoosi.clfd.framework.util.Constants;
import org.springframework.stereotype.Service;

@Service(value="siteService")
public class SiteServiceImpl extends BaseServiceImpl<Site, SiteSearch, SiteMapper> implements SiteService {
	
	@Resource (name="langService")
	private LangService langService;
	
	@Resource (name="cmsMenuService")
	private MenuService menuService;
	
	@Override
	@Resource (name="siteMapper")
	protected void setMapper (SiteMapper mapper) {
		super.setMapper (mapper);
	}

	@Override
	public List<Site> selectList(SiteSearch s) {
		List<Site> resultList = new ArrayList<Site>();
		List<Site> siteList = super.selectList(s);
		LangSearch langSearch;
		for(Site site : siteList) {
			langSearch = new LangSearch();
			langSearch.setLangId(site.getLangId());
			site.setLang(langService.select(langSearch));
			resultList.add(site);
		}
		
		return resultList;
	}

	@Override
	public List<Site> selectJson() {
		if(ApplicationHelper.getSiteList() == null) {
			initJson();
		}
		
		return ApplicationHelper.getSiteList();
	}
	
	@Override
	public void initJson() {
		SiteSearch siteSearch = new SiteSearch();
		ApplicationHelper.setSiteList(selectList(siteSearch));
	}

	@Override
	public void insert(Site site) {
		super.insert(site);
		
		//메인 메뉴 등록
		Menu menu = new Menu();
		menu.setSiteId(site.getSiteId());
		menu.setMenuNm("메인");
		menu.setMenuUrl("main");
		menu.setMenuOrd(0);
		menu.setMenuTp(Constants.MENU_TYPE_PGM);
		menu.setUseSt("Y");
		menu.setRegId(site.getRegId());
		
		menuService.insert(menu);
		menuService.initJson(site.getSiteId());
	}
	
}
