package com.dbnt.faisp.translator;

import com.dbnt.faisp.menuMgt.mapper.MenuMgtMapper;
import com.dbnt.faisp.menuMgt.model.MenuMgt;
import com.dbnt.faisp.menuMgt.repository.MenuMgtRepository;
import com.dbnt.faisp.translator.mapper.TranslatorMapper;
import com.dbnt.faisp.translator.model.Translator;
import com.dbnt.faisp.translator.model.Translator.TranslatorId;
import com.dbnt.faisp.translator.repository.TranslatorRepository;
import com.dbnt.faisp.userInfo.model.UserInfo;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Transient;

import java.time.LocalDateTime;
import java.util.*;

@Service
@RequiredArgsConstructor
public class TranslatorService {

	private final TranslatorRepository translatorRepository;
	private final TranslatorMapper translatorMapper;

	public String insertTranslatorInfo(Translator translator) {
		Translator dbTranslator = translatorRepository.findFirstByOrderByTranslatorKeyDesc();
		translator.setWrtDt(LocalDateTime.now());
		if (dbTranslator == null) {
			translator.setTranslatorKey(1);
			translator.setVersionNo(1);
			translatorRepository.save(translator);
			return translatorRepository.save(translator).getTrName();
		} else {
			translator.setTranslatorKey(dbTranslator.getTranslatorKey() + 1);
			translator.setVersionNo(1);
			return translatorRepository.save(translator).getTrName();
		}

	}

	public List<Translator> selectTranslatorList(Translator translator) {
		return translatorMapper.selectTranslatorList(translator);
	}

	public Integer selectTranslatorListCnt(Translator translator) {
		return translatorMapper.selectTranslatorListCnt(translator);
	}

	public Translator selectTranslatorView(Translator translator) {
		return translatorRepository.findById(new TranslatorId(translator.getTranslatorKey(), translator.getVersionNo()))
				.orElse(null);
	}

	@Transactional
	public void updatetranslatorInfo(Translator translator) {
		Translator dbTranslator = translatorRepository
				.findById(new TranslatorId(translator.getTranslatorKey(), translator.getVersionNo())).orElse(null);
		Translator translatorTmp = new Translator();
		translatorTmp.setTranslatorKey(dbTranslator.getTranslatorKey());
		translatorTmp.setVersionNo(dbTranslator.getVersionNo() + 1);
		translatorTmp.setOgdp1(translator.getOgdp1());
		translatorTmp.setTrLang(translator.getTrLang());
		translatorTmp.setTrCareer(translator.getTrCareer());
		translatorTmp.setTrName(translator.getTrName());
		translatorTmp.setTrSex(translator.getTrSex());
		translatorTmp.setTrPhone(translator.getTrPhone());
		translatorTmp.setTrNny(translator.getTrNny());
		translatorTmp.setTrAge(translator.getTrAge());
		translatorTmp.setTrEdu(translator.getTrEdu());
		translatorTmp.setTrCft(translator.getTrCft());
		translatorTmp.setTrVisa(translator.getTrVisa());
		translatorTmp.setAptDt(translator.getAptDt());
		translatorTmp.setDmlYn(translator.getDmlYn());
		translatorTmp.setRemark(translator.getRemark());
		translatorTmp.setWrtNm(translator.getWrtNm());
		translatorTmp.setWrtDt(LocalDateTime.now());
		translatorTmp.setWrtOrgan(translator.getWrtOrgan());
		translatorRepository.save(translatorTmp);
	}

}
