package com.eactive.eai.transformer.dao.excel;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.data.entity.onl.transformer.layout.LayoutTypeEntity;
import com.eactive.eai.transformer.dao.TransformerDaoException;
import com.eactive.eai.transformer.layout.LayoutType;
import com.eactive.eai.transformer.layout.loader.LayoutTypeLoader;
import com.eactive.eai.transformer.layout.mapper.LayoutTypeMapper;

@Service
@Transactional
public class LayoutTypeExcelDao extends BaseExcelDao<String, LayoutType> {

	private static final int DATA_ROW_OFFSET = 10;
	private static final int COL_NAME = 1;
	private static final int COL_DESC = 2;
	private static final int COL_CLAS = 3;

	@Autowired
	private LayoutTypeLoader layoutTypeLoader;

	@Autowired
	private LayoutTypeMapper layoutTypeMapper;

	public LayoutTypeExcelDao() throws TransformerDaoException {
		super("layout_type");
	}

	protected Map<String, LayoutType> parse(Sheet[] sheets) throws TransformerDaoException {
		Map<String, LayoutType> map = new TreeMap<>();

		Sheet sheet = sheets[0];

		LayoutType layoutType = null;

		try {
			for (int currentRow = DATA_ROW_OFFSET; sheet.existsRow(currentRow); currentRow++) {
				try {
					String name = sheet.getCellString(currentRow, COL_NAME);

					if (name.trim().length() > 0) {
						layoutType = new LayoutType();
						layoutType.setName(name);
						layoutType.setDescription(sheet.getCellString(currentRow, COL_DESC));
						layoutType.setGenClass(sheet.getCellString(currentRow, COL_CLAS));

						/**
						 * set overwrite
						 */
						boolean overwrite = sheet.isEquals(6, 4, "O");
						layoutType.setOverwrite(overwrite);

						map.put(layoutType.getName(), layoutType);
					} else {
						break;
					}
				} catch (Exception e) {
					logger.error("[ERROR] !!!!! " + e.getMessage() + " - ROW: " + currentRow, e);
				}
			}
		} catch (Exception e) {
			throw new TransformerDaoException(e.getMessage(), e);
		}

		return map;
	}

	/**
	 * LayoutTypeDao Interface
	 */
	public LayoutType findLayoutTypeByName(String typeName) throws TransformerDaoException {
		return null;
	}

	/**
	 * ExcelJob Interface
	 */
	protected Map<String, LayoutType> loadSheets(Sheet[] sheets) throws TransformerDaoException {
		Map<String, LayoutType> map = parse(sheets);
		return map;
	}

	@SuppressWarnings("rawtypes")
	public void save(Map layouttypeMap) {
		if (logger.isInfoEnabled()) {
			logger.info("    + save layout types");
		}

		for (Iterator iter = layouttypeMap.values().iterator(); iter.hasNext();) {
			LayoutType layoutType = (LayoutType) iter.next();
			if (layoutType == null) {
				continue;
			}

			if (layoutType.getOverwrite()) {
				layoutTypeLoader.findById(layoutType.getName()).ifPresent(layoutTypeLoader::delete);
			}

			
			LayoutTypeEntity layoutTypeEntity = layoutTypeMapper.toEntity(layoutType);
			layoutTypeLoader.save(layoutTypeEntity);
			
		}
	}

	@Override
	protected Map<String, LayoutType> removeSheets(Sheet[] sheets) throws TransformerDaoException {
		try {
			layoutTypeLoader.deleteAll(layoutTypeLoader.findAll());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

}
