/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.HotelServices.model.dto.impl;

import com.HotelServices.model.dao.RecepciosDAO;
import com.HotelServices.model.dto.RecepciosDTO;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.LinkedList;

/**
 *
 * @author Sensei
 */
public class RecepciosDAOImpl extends AbstractGenericDAOImpl<RecepciosDTO, Long>
	implements RecepciosDAO{

    public boolean insert(RecepciosDTO entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean deleteById(Long entityId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean ujVendeg(String felhasznalonev, String jelszo, String jelszoUjra, String nev, int iranyitoszam, String cim, String telszam, int szobaszam, int napok) {
        Connection con = null;
        if (jelszo.equals(jelszoUjra)){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelservices", "root", "admin");
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("SELECT max(idVendeg) FROM vendeg");
            int i = 0;
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                i = rs.getInt(1) + 1;
                System.out.println(i);
            }

            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM vendeg where FelhasznaloNev = ?");
            stmt.setString(1, felhasznalonev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                con.close();
                return false;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM recepcios where FelhasznaloNev = ?");
            stmt.setString(1, felhasznalonev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                con.close();
                return false;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szakacs where FelhasznaloNev = ?");
            stmt.setString(1, felhasznalonev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                con.close();
                return false;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM takarito where FelhasznaloNev = ?");
            stmt.setString(1, felhasznalonev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                con.close();
                return false;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szallodavezeto where FelhasznaloNev = ?");
            stmt.setString(1, felhasznalonev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                con.close();
                return false;
            } 
            stmt = (PreparedStatement) con.prepareStatement("INSERT INTO vendeg VALUES(?,?,?,?,?,?,?)");
            stmt.setLong(1, new Long(i));
            stmt.setString(2, felhasznalonev);
            stmt.setString(3, jelszo);
            stmt.setString(4, nev);
            stmt.setInt(5, iranyitoszam);
            stmt.setString(6, cim);
            stmt.setString(7, telszam);
            stmt.execute();
            
            stmt = (PreparedStatement) con.prepareStatement("SELECT max(idSzoba_Vendeg) FROM szoba_vendeg");
            int j = 0;
            rs = stmt.executeQuery();
            if (rs.next()) {
                j = rs.getInt(1) + 1;
                System.out.println(j);
            }
            
            stmt = (PreparedStatement) con.prepareStatement("INSERT INTO szoba_vendeg VALUES(?,?,?,?)");
            stmt.setLong(1, new Long(j));
            stmt.setInt(2,napok);
            stmt.setInt(3,szobaszam);
            stmt.setInt(4,i);
            stmt.execute();
            
            
            stmt.close();
            con.close();
            return true;
         
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
        }
        return false;
    }

    public boolean vendegTorol() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean takaritasRegisztracio(String usernev, int szobaszam, String leiras) {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelservices", "root", "admin");
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("SELECT max(idTakaritas) FROM takaritas");
            int i = 1, j = -1;
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                i = rs.getInt(1) + 1;
//                System.out.println(i);
            }
            
            j = (new Bejelentkezik().userAzon(usernev))-100;
            System.out.println("i: "+i);
            System.out.println("j: "+j);
            
            stmt = (PreparedStatement) con.prepareStatement("INSERT INTO takaritas VALUES(?,?,?,?)");
            stmt.setLong(1, new Long(i));
            stmt.setInt(2, szobaszam);
            stmt.setString(3, leiras);
            stmt.setInt(4, j);
            stmt.execute();
            
            stmt.close();
            con.close();
            return true;
         
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        return false;
    } 
    }

    public LinkedList<VendegSzoba> vendegek() {
        LinkedList<VendegSzoba> ll = new LinkedList<VendegSzoba>();
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelservices", "root", "admin");
            
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("select v.FelhasznaloNev, v.Nev, sz.Szoba_idSzoba FROM szoba_vendeg sz, vendeg v WHERE idVendeg = Vendeg_idVendeg");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                VendegSzoba uj = new VendegSzoba();
                uj.setFelhsznaloNev(rs.getString(1));
                uj.setNev(rs.getString(2));
                uj.setSzoba(rs.getInt(3));
                
//                System.out.println(szoba.getAr());
                ll.addLast(uj);
                
            }
            con.close();
            stmt.close();

        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
    return ll;     
    }
    
    
}