package EntitiesDao;

import HibernateEntities.ClientsEntity;
import org.hibernate.Session;
import org.hibernate.query.Query;

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

import static HibernateUtil.HibernateUtil.getSession;

/**
 * Created by Кинетик on 31.12.2016.
 */
public class ClientsDao {
    public static HashMap<String, String> getClientMap(){
        Session session = getSession();
        Query query = session.createQuery("FROM ClientsEntity ");
        List<ClientsEntity> res = query.list();
        HashMap<String,String> clientsDict = new HashMap<>();
        for(ClientsEntity cl: res){
            clientsDict.put(cl.getClFamily()+" "+cl.getClFirstName()+" "+cl.getClSecName(),cl.getClPassword());
        }
        session.close();
        return clientsDict;
    }
    public static ArrayList<String> getClientList(){
        Session session = getSession();
        Query query = session.createQuery("FROM ClientsEntity ");
        List<ClientsEntity> res = query.list();
        ArrayList<String> clientsList = new ArrayList<>();
        for(ClientsEntity cl: res){
            clientsList.add(cl.getClFamily()+" "+cl.getClFirstName()+" "+cl.getClSecName());
        }
        session.close();
        return clientsList;
    }
    public static void createNewClient(ClientsEntity client) {
        Session session = getSession();
        Query query = session.createQuery("Select max(clientId) FROM ClientsEntity ");
        Integer res = (Integer)query.uniqueResult();
        client.setClientId(res+1);
        session.beginTransaction();
        session.save(client);
        session.getTransaction().commit();
        session.close();
    }
    public static boolean checkIsClient(ClientsEntity client){
        Session session = getSession();
        Query query = session.createQuery("FROM ClientsEntity WHERE clFamily="+client.getClFamily()+" and clFirstName="+client.getClFirstName()+" and clSecName="+client.getClSecName());
        List<ClientsEntity> res = query.list();
        if(res.size()==0){
            return false;
        }
        return true;
    }
}
