package com.palmnest.service.repair.impl;

import com.palmnest.dao.ElementGroupDao;
import com.palmnest.dao.OrganizationDao;
import com.palmnest.dao.UserDao;
import com.palmnest.dao.repair.UserRepairTypeDao;
import com.palmnest.model.Element;
import com.palmnest.model.User;
import com.palmnest.model.repair.UserRepairType;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.repair.UserRepairTypeManager;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("userRepairTypeManager")
public class UserRepairTypeManagerImpl extends GenericManagerImpl<UserRepairType, Long> implements UserRepairTypeManager {


	private UserRepairTypeDao userRepairTypeDao;
	@Autowired
	private UserDao           userDao;
	@Autowired
	private ElementGroupDao   elementGroupDao;
	@Autowired
	private OrganizationDao   organizationDao;

	@Autowired
	public void setUserRepairTypeDao(UserRepairTypeDao userRepairTypeDao) {
		this.userRepairTypeDao = userRepairTypeDao;
		this.dao = userRepairTypeDao;
	}

	@Override
	public List<User> findBy(Long repairTypeId, Long roleId) {
		List<UserRepairType> list  = userRepairTypeDao.findBy(repairTypeId, roleId);
		List<User>           users = new ArrayList<User>();
		for (UserRepairType r : list) {
			User user = userDao.get(r.getUserId());
			if (user.getIsenabled() != null && user.getIsenabled().equals("1")) {
				users.add(userDao.get(r.getUserId()));
			}
		}
		return users;
	}

	@Override
	public List<Element> findByUser(Long userId) {
		List<UserRepairType> list = userRepairTypeDao.findByUser(userId);
		List<Element>        type = new ArrayList<Element>();
		for (UserRepairType r : list) {
			Element element = elementGroupDao.getElementId(r.getRepairTypeId());
			if (element != null) {
				type.add(element);
			}

		}
		return type;
	}

	@Override
	public UserRepairType findBy(Long userId, Long repairTypeId, Long roleId) {
		return userRepairTypeDao.findBy(userId, repairTypeId, roleId);
	}

	@Override
	public List<UserRepairType> findByUserAndRole(Long userId, Long roleId) {
		return userRepairTypeDao.findByUser(userId, roleId);
	}

	@Override
	public List<User> findByRole(Long repairTypeId, Long roleId) {
		List<UserRepairType> list  = userRepairTypeDao.findBy(repairTypeId, roleId);
		List<User>           users = new ArrayList<User>();
		for (UserRepairType r : list) {
			User user = userDao.get(r.getUserId());
			if (user.getIsenabled() != null && user.getIsenabled().equals("1")) {
				users.add(userDao.get(r.getUserId()));
			}
		}
		return users;
	}

	@Override
	public List<User> findBy(Long repairTypeId) {
		List<UserRepairType> list  = userRepairTypeDao.findBy(repairTypeId);
		List<User>           users = new ArrayList<User>();
		for (UserRepairType r : list) {
			User user = userDao.get(r.getUserId());
			if (user.getIsenabled() != null && user.getIsenabled().equals("1") && !users.contains(user)) {
				users.add(user);
			}
		}
		return users;
	}

	@Override
	public List<Element> findByUser(Long userId, Long roleId) {
		List<UserRepairType> list = userRepairTypeDao.findByUser(userId, roleId);
		List<Element>        type = new ArrayList<Element>();
		for (UserRepairType r : list) {
			type.add(elementGroupDao.getElementId(r.getRepairTypeId()));
		}
		return type;
	}

	@Override
	public UserRepairType findByRepair(Long userId, Long repairTypeId) {
		return userRepairTypeDao.findByRepair(userId, repairTypeId);
	}

	@Override
	public List<User> findBy(Long repairTypeId, Integer orgId) {
		List<Map<String, Object>> list  = userRepairTypeDao.findBy(repairTypeId, orgId);
		List<User>                users = new ArrayList<User>();
		Map<Long,Object> objectMap =new HashedMap();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map  = list.get(i);
			User                user = userDao.get(Long.parseLong(map.get("user_id").toString()));
//			List<TbOrganization> organizationList = new ArrayList<TbOrganization>();
			try {
//				organizationList = organizationManager.getUserRelationOrganizationList(user.getId());
//				if(user != null && user.isAdmin()) {
//					organizationList = organizationDao.getAllOrganization();
//				}
//				List<TbOrganization> organizations=new ArrayList<TbOrganization>();
//				List<AppUserOrgnizationRelation> relaitons=userDao.getUserOrgByUserId(user.getId());
//				for (AppUserOrgnizationRelation relation : relaitons) {
//					TbOrganization org = organizationDao.getOrganizationById(relation.getOrgId());
//					if ("1".equals(org.getIsEnabled())&&"1".equals(org.getStatus()))
//						organizations.add(org);
//				}
//				String organizationId="#";
//				if(organizationList.size()>=1){
//					organizationId=organizationList.get(0).getId().toString();
//				}
				if (objectMap.get(user.getId()) == null && user.getIsenabled() != null && user.getIsenabled().equals("1")) {
					users.add(user);
					objectMap.put(user.getId(), user);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return users;
	}

	@Override
	public int removeByUser(Long userId, Long roleId) {
		return userRepairTypeDao.removeByUser(userId, roleId);
	}

	@Override
	public int removeByRoleId(Long roleId) {
		return userRepairTypeDao.removeByRoleId(roleId);
	}
}
