package org.mynonglam.dao;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.Admin;
import org.mynonglam.entitys.Group;

public class AdminDAO {
	private static SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public boolean chekLogin(Admin admin){
		boolean result = false;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Admin");
		List<Admin> listAdmin = query.list();
		for(int i= 0; i < listAdmin.size(); i++){
			if(listAdmin.get(i).getAdminname().equals(admin.getAdminname())){
				if(listAdmin.get(i).getAdminpass().equals(admin.getAdminpass())){
					result = true;
				}
			}
		}
		session.close();
		return result;
	}
	public boolean checkPass(String pass, String user){
		boolean result = false;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Admin ad where ad.adminname=:adminname");
		query.setString("adminname", user);
		List<Admin> list = query.list();
		if(list.get(0).getAdminpass().equals(pass)){
			result = true;
		}
		session.close();
		return result;
	}
	public void updatePass(String newPass, String user ){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("update from Admin ad set ad.adminpass=:adminpass where ad.adminname=:adminname");
		query.setString("adminpass", newPass);
		query.setString("adminname", user);
		query.executeUpdate();
		session.close();
	}
	public static List<Account> getListAcc(){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Account");
		List<Account> listAcc = query.list();
		session.close();
		return listAcc;
	}
	public void updateIsblock(String email, String status){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("update from Account acc set acc.isblock=:isblock where acc.email=:email");
		boolean stas = Boolean.parseBoolean(status);
		if(stas){
			query.setBoolean("isblock", false);
		}
		else{
			query.setBoolean("isblock", true);
		}
		query.setString("email", email);
		query.executeUpdate();
		session.close();
	}
	public static List<Group> getListGroup(){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Group");
		List<Group> listGroup = query.list();
		session.close();
		return listGroup;
	}
	public void blockGroup(String idGroup, String status){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("update from Group g set g.isblock=:isblock where g.idgroup=:idgroup");
		boolean stas = Boolean.parseBoolean(status);
		if(stas){
			query.setBoolean("isblock", false);
		}
		else{
			query.setBoolean("isblock", true);
		}
		query.setString("idgroup", idGroup);
		query.executeUpdate();
		session.close();
	}
}
