package EntitiesDao;

import HibernateEntities.ClientsEntity;
import HibernateEntities.ModeratorsEntity;
import HibernateEntities.StudentsEntity;
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 AdminDao {
    public static HashMap<String, String> getAdminMap(){
        Session session = getSession();
        Query query = session.createQuery("FROM ModeratorsEntity ");
        List<ModeratorsEntity> res = query.list();
        HashMap<String,String> adminsDict = new HashMap<>();
        for(ModeratorsEntity ad: res){
            adminsDict.put(ad.getModFamily()+" "+ad.getModFirstName()+" "+ad.getModSecName(),ad.getModPassword());
        }
        session.close();
        return adminsDict;
    }
    public static ArrayList<String> getAdminList(){
        Session session = getSession();
        Query query = session.createQuery("FROM ModeratorsEntity ");
        List<ModeratorsEntity> res = query.list();
        ArrayList<String> adminsList = new ArrayList<>();
        for(ModeratorsEntity ad: res){
            adminsList.add(ad.getModFamily()+" "+ad.getModFirstName()+" "+ad.getModSecName());
        }
        session.close();
        return adminsList;
    }
    public static void createNewAdmin(ModeratorsEntity admin) {
        Session session = getSession();
        Query query = session.createQuery("select max(modId) from ModeratorsEntity ");
        Integer res = (Integer) query.uniqueResult();
        admin.setModId(res+1);
        session.beginTransaction();
        session.save(admin);
        session.getTransaction().commit();
        session.close();
    }
    public static boolean checkIsAdmin(ModeratorsEntity admin){
        Session session = getSession();
        Query query = session.createQuery("FROM ModeratorsEntity WHERE modFamily="+admin.getModFamily()+ " and modFirstName="+admin.getModFirstName()+" and modSecName="+admin.getModSecName()+" and modBirthdate="+admin.getModBirthdate());
        List<ModeratorsEntity> res = query.list();
        if(res.size()==0){
            return false;
        }
        return true;
    }
}
