package com.ffii.lioner.modules.lioner.commonField.service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.ffii.core.support.AbstractBaseEntityService;
import com.ffii.core.support.JdbcDao;
import com.ffii.core.utils.BeanUtils;
import com.ffii.lioner.modules.common.service.AuditLogService;
import com.ffii.lioner.modules.lioner.commonField.entity.CommonField;
import com.ffii.lioner.modules.lioner.commonField.entity.CommonFieldRepository;
import com.ffii.lioner.modules.lioner.commonField.req.UpdateCommonFieldReq;

@Service
public class CommonFieldService extends AbstractBaseEntityService<CommonField, Long, CommonFieldRepository> {

	private AuditLogService auditLogService;
	public CommonFieldService(JdbcDao jdbcDao, CommonFieldRepository repository, AuditLogService auditLogService) {
		super(jdbcDao, repository);
		this.auditLogService = auditLogService;
	}

	public Map<String,Object> getAuditLogObject(Map<String,Object> req){
		StringBuilder sql = new StringBuilder("SELECT"
			+ " e.id,    "
			+ " e.created,    "
			+ " e.createdBy AS createdById,    "
			+ " u1.name AS createdBy,    "
			+ " e.version,    "
			+ " e.modified,    "
			+ " e.modifiedBy AS modifiedById,    "
			+ " e.deleted, "
			+ " u2.name AS modifiedBy,    "
			+ " e.name, "
			+ " e.nameCht, "
			+ " e.description, "
			+ " e.region, "
			+ " e.organization, "
			+ " e.eventType, "
			+ " e.frequency, "
			+ " e.series, "
			+ " e.startDate, "
			+ " e.eventFrom, "
			+ " e.eventTo, "
			+ " e.applicationDeadline, "
			+ " e.nextApplicationDate, "
			+ " e.announcementDate, "
			+ " e.awardDate, "
			+ " e.reminderFlag, "
			+ " e.reminderThreshold, "
			+ " e.reminderInterval, "
			+ " e.reminderLimit  "
			+ " FROM event e 	   "
			+ " LEFT JOIN `user` u1 ON u1.id = e.createdBy     "
			+ " LEFT JOIN `user` u2 ON u2.id = e.modifiedBy       "
			+ " WHERE e.id = :id    "
		);
		return jdbcDao.queryForMap(sql.toString(), req).get();
	}

	public List<Map<String,Object>> list(Map<String, Object> args){
			StringBuilder sql = new StringBuilder("SELECT"
				+ " common_field.* "
				+ " FROM common_field "
				+ " WHERE deleted = FALSE "
			// StringBuilder sql = new StringBuilder("SELECT"
			// 	+ " DISTINCT e.id, "
			// 	+ " e.name AS name, "
			// 	+ " e.applicationDeadline, "
			// 	+ " e.startDate, "
			// 	+ " e.eventFrom "
			// 	+ " FROM event e "
			// 	+ " LEFT JOIN event_sub_division esd ON e.id = esd.eventId "
			// 	+ " LEFT JOIN sub_division sd ON esd.subDivisionId = sd.id "
			// 	+ " LEFT JOIN division d ON d.id = sd.divisionId "
			// 	+ " LEFT JOIN `user` u ON u.id = e.createdBy "
			// 	+ " WHERE e.deleted = FALSE "
			// 	+ " AND esd.deleted = FALSE "

		);

		if (args != null) {
			if (args.containsKey("fullname")) 
				sql.append(" AND fullname LIKE :fullname ");
			if (args.containsKey("lastname")) 
				sql.append(" AND lastname LIKE :lastname ");
			if (args.containsKey("firstname")) 
				sql.append(" AND firstname LIKE :firstname ");
			if (args.containsKey("phone1")) 
				sql.append(" AND phone1 LIKE :phone1 ");
			if (args.containsKey("phone2")) 
				sql.append(" AND phone2 LIKE :phone2 ");
			if (args.containsKey("email")) 
				sql.append(" AND email LIKE :email ");
			if (args.containsKey("remarks")) 
				sql.append(" AND remarks LIKE :remarks ");
			if (args.containsKey("joinDateFrom")) 
				sql.append(" AND joinDate >= :joinDateFrom ");
			if (args.containsKey("joinDateTo")) 
				sql.append(" AND joinDate <= :joinDateTo ");
			// if (args.containsKey("type")) 
			// 	sql.append(" AND eventType LIKE :type ");
			// if (args.containsKey("divisionIdList")) 
			// 	sql.append(" AND sd.divisionId IN (:divisionIdList) ");
			// if (args.containsKey("subDivisionIdList")) 
			// 	sql.append(" AND sd.id IN (:subDivisionIdList) ");
			// if (args.containsKey("keyword")){
			// 	sql.append(" AND ( createdBy LIKE :keyword ");
			// 	sql.append(" OR u.username  LIKE :keyword ");
			// 	sql.append(" OR u.fullname LIKE :keyword ");
			// 	sql.append(" OR u.email LIKE :keyword ");
			// 	sql.append(" OR sd.name LIKE :keyword ");
			// 	sql.append(" OR d.name LIKE :keyword ");
			// 	sql.append(" OR e.name LIKE :keyword) ");
			}
				
		// }
		// sql.append(" ORDER BY id desc ");

		return jdbcDao.queryForList(sql.toString(), args);
	}

	public long getIdByClientId(long clientId) {
        String sql = "SELECT"
			+ " cf.id "
			+ " FROM common_field cf  "
			+ " WHERE cf.deleted = FALSE"
			+ " AND cf.clientId = :clientId ";
        
        return (long)jdbcDao.queryForInt(sql, Map.of("clientId", clientId));
    }

	public Long getByClientId(long clientId){
		Long id =  getIdByClientId(clientId); 
		return id;
	}

	
	public Map<String, Object> saveOrUpdate(UpdateCommonFieldReq req) {
		CommonField instance;
		// List<SubDivision> onUsingIdList = new ArrayList<SubDivision>();
		if(req.getId()>0){
			instance = find(req.getId()).get();
			// onUsingIdList = this.getSelectedSubDivisionList(req.getId());
		}
		else{
			instance = new CommonField();
		}

		BeanUtils.copyProperties(req,instance);
		
		instance = save(instance);
				
		return Map.of(
			"id", instance.getId()
		);
	}
}