package com.nisum.mytime.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.mongodb.WriteResult;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Domains;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.DomainRepo;

import com.nisum.mytime.utils.MyTimeUtils;


/**
 * @author Vijay
 *
 */
@Service
public class DomainServiceImpl implements DomainService {
	
	List<Domains> domainList = null;
	List<HashMap<Object, Object>> updatedDomainList = null;
	List<HashMap<String, String>> updatedEmployeeList = null;

    @Autowired
    private DomainRepo domainRepo;
    
    @Autowired
	private MongoTemplate mongoTemplate;
   
    @Override
    public Domains addDomain(Domains d) throws MyTimeException{
        return domainRepo.save(d);
    }
    
    @Override
    public List<HashMap<Object, Object>> getAllDomains() throws MyTimeException {

    	domainList=domainRepo.findAll();
    	updatedDomainList = new ArrayList<>();
		for (Domains domain : domainList) {
			HashMap<Object, Object> domainMap = new HashMap<>();
			domainMap.put(MyTimeUtils.ID,domain.getId());
			domainMap.put(MyTimeUtils.DOMAIN_ID, domain.getDomainId());
			domainMap.put(MyTimeUtils.DOMAIN_NAME,domain.getDomainName());
			domainMap.put(MyTimeUtils.ACCOUNT_NAME,domain.getAccountName());
			domainMap.put(MyTimeUtils.STATUS,domain.getStatus());

			updatedEmployeeList = new ArrayList<>();
			List<String> employeeIds=domain.getDeliveryManagers();

			Query query = new Query(Criteria.where(MyTimeUtils.EMPLOYEE_ID).in(employeeIds));
			List<EmployeeRoles> employeeRoles = mongoTemplate.find(query, EmployeeRoles.class);

			for(EmployeeRoles employeesRole : employeeRoles){
				HashMap<String, String> employeeMap = new HashMap<>();	
				employeeMap.put(MyTimeUtils.EMPLOYEE_ID,employeesRole.getEmployeeId());
				employeeMap.put(MyTimeUtils.EMPLOYEE_NAME,employeesRole.getEmployeeName());
				updatedEmployeeList.add(employeeMap);
			}	
			domainMap.put(MyTimeUtils.DELIVERY_MANAGERS,updatedEmployeeList);
			updatedDomainList.add(domainMap);
		}
    	return updatedDomainList;
    }

    @Override
    public WriteResult deleteDomain(String id) throws MyTimeException{
    	Query query = new 	Query(Criteria.where(MyTimeUtils.DOMAIN_ID).is(id));
    	Update update = new Update(); 
    	update.set(MyTimeUtils.STATUS,MyTimeUtils.IN_ACTIVE);
    	return mongoTemplate.upsert(query, update,Domains.class);
    }


	@Override
	public List<Domains> validateDomains(String domainName, String accountName) throws MyTimeException {
		List<Domains> domainList=domainRepo.findByDomainNameAndAccountName(domainName,accountName);
		return domainList;
	}
    

}