package com.acme.model.domain.ad.dao;


import com.acme.model.domain.ad.entity.Branch;
import com.acme.structure.BaseEntity;
import com.acme.structure.dao.MainDao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Query;


public class ADDao
{
    MainDao dao = null;

    public ADDao()
    {
        dao = new MainDao();
    }

    public Collection<BaseEntity> findDepartmentsForBranch(EntityManager manager, Branch branch)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT d FROM Branch b INNER JOIN b.departments d WHERE b.code=:code \n");
        Query query = manager.createQuery(queryString.toString());

        query.setParameter("code", branch.getCode());

        return query.getResultList();
    }

    public Branch findBranchForPrincipal(EntityManager manager, String principal)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT e.branch FROM Employee e  WHERE e.user.username=:principal \n");

        Query query = manager.createQuery(queryString.toString());

        query.setParameter("principal", principal);

        return (Branch)query.getSingleResult();
    }

    public Collection<BaseEntity> findBranchsForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.Branch(b.id, b.name) \n").append("FROM Branch b \n").append("ORDER BY b.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }

    public Collection<BaseEntity> findDepartmentsForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.Department(d.id, d.name) \n").append("FROM Department d \n").append("ORDER BY d.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }

    public Collection<BaseEntity> findCitiesForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.City(c.id, c.name) \n").append("FROM City c \n").append("ORDER BY c.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }

    public Collection<BaseEntity> findDistrictsForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.District(d.id, d.name) \n").append("FROM District d \n").append("ORDER BY d.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }

    public Collection<BaseEntity> findUnitGroupsForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.UnitGroup(ug.id, ug.name) \n").append("FROM UnitGroup ug \n").append("ORDER BY ug.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }

    public Collection<BaseEntity> findCurrenciesForList(EntityManager manager)
    {
        StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT new com.acme.model.domain.ad.entity.Currency(c.id, c.name) \n").append("FROM Currency c \n").append("ORDER BY c.orderNo, c.name \n");

        Query query = manager.createQuery(queryString.toString());

        return query.getResultList();
    }
}
