package com.style24.admin.biz.service;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

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

import com.style24.admin.biz.dao.TsaPollDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.Poll;

import lombok.extern.slf4j.Slf4j;

/*
* poll Service
* 
* @author sowon
* @since 2021. 02. 19
*/

@Service
@Slf4j
public class TsaPollService {
	
	@Autowired
	private TsaPollDao pollDao;
	
	/**
	 * POLL 등록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 2. 19
	 */
	@Transactional("shopTxnManager")
	public void createPoll(Collection<Poll> params) {
		int userNo = TsaSession.getInfo().getUserNo();

		// POLL 마스터
		Poll pollInfo = new Poll();
		pollInfo.setPollSqTemp(params.iterator().next().getPollSq());
		pollInfo.setPollTitle(params.iterator().next().getPollTitle());
		pollInfo.setSiteCd(params.iterator().next().getSiteCd());
		pollInfo.setDupPartiYn(params.iterator().next().getDupPartiYn());
		pollInfo.setFstComYn(params.iterator().next().getFstComYn());
		pollInfo.setPartiCnt(params.iterator().next().getPartiCnt());
		pollInfo.setPollStdt(params.iterator().next().getPollStdt());
		pollInfo.setPollEddt(params.iterator().next().getPollEddt());
		pollInfo.setRegNo(userNo);
		pollInfo.setUpdNo(userNo);
		
		pollDao.savePoll(pollInfo);

		int pollSq = params.iterator().next().getPollSq() == null ? pollInfo.getPollSqTemp() : params.iterator().next().getPollSq();

		// POLL 문항
		//Poll deletePollQuestion = new Poll();
		//deletePollQuestion.setPollSq(pollSq);  
		//pollDao.deletePollQuestion(deletePollQuestion);

		int dispOrd = 1;
		
		for (Poll pollQuestion : params) {
			pollQuestion.setPollSq(pollSq);
			pollQuestion.setRegNo(userNo);
			pollQuestion.setUpdNo(userNo);
			pollQuestion.setDispOrd(dispOrd);
			pollDao.createPollQuestion(pollQuestion);
			dispOrd++;
		}
	}
	
	/**
	 * POLL 문항 삭제
	 *
	 * @param Poll 문항 정보
	 * @author sowon
	 * @since 2021. 5. 2
	 */
	@Transactional("shopTxnManager")
	public void removeQuestion(Poll poll) {
		pollDao.removeQuestion(poll);
	}

	/**
	 * POLL 목록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 2. 19
	 */
	public Collection<Poll> getPollList(Poll poll) {
		return pollDao.getPollList(poll);
	}
	
	/**
	 * POLL 투표자 목록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 2. 22
	 */
	public Collection<Poll> getPollVoterList(Poll poll) {
		return pollDao.getPollVoterList(poll);
	}
	
	/**
	 * POLL 보기 목록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 2. 22
	 */
	public Collection<Poll> getPollQuestion(Poll poll) {
		return pollDao.getPollAnswer(poll);
	}
	
	/**
	 * POLL ANSWER조회
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 2. 22
	 */
	public Collection<Poll> getPollAnswer(Poll poll) {
		return pollDao.getPollAnswer(poll);
	}

	/**
	 * POLL ALL QUESTION 조회
	 *
	 * @param Poll
	 * @return POLL
	 * @author sowon
	 * @since 2021. 6. 2
	 */
	public Collection<Poll> getPollAllQuestionList(Poll poll){
		return pollDao.getPollAllQuestionList(poll);
	}

}
