/*
 * 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.VendegDAO;
import com.HotelServices.model.dto.EtelDTO;
import com.HotelServices.model.dto.SzobaDTO;
import com.HotelServices.model.dto.VendegDTO;
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 VendegDAOImpl extends AbstractGenericDAOImpl<VendegDTO, Long>
        implements VendegDAO {

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

    public boolean deleteById(Long entityId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public boolean regisztracio(String felhasznalonev, String jelszo, String jelszoUjra, String nev, int iranyitoszam, String cim, String telszam) {
        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.close();
            con.close();
            return true;
         
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
        }
        return false;
    }

    public LinkedList<SzobaDTO> szobak() {
        LinkedList<SzobaDTO> ll = new LinkedList<SzobaDTO>();
        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 * FROM szoba");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                SzobaDTO szoba = new SzobaDTO();
                szoba.setId_szoba(rs.getInt(1));
                szoba.setFerohely(rs.getInt(2));
                szoba.setAr(rs.getInt(3));
//                System.out.println(szoba.getAr());
                ll.addLast(szoba);
                
            }
            con.close();
            stmt.close();

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

    public LinkedList<EtelDTO> etel() {
        LinkedList<EtelDTO> ll = new LinkedList<EtelDTO>();
        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 * FROM etel");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                EtelDTO etel = new EtelDTO();
                etel.setId(rs.getInt(1));
                etel.setNap(rs.getString(2));
                etel.setMenu(rs.getString(3));
                etel.setEloetel(rs.getString(4));
                etel.setFoetel(rs.getString(5));
                etel.setDesszert(rs.getString(6));
                etel.setSzakacs_id(rs.getInt(7));
//                System.out.println(szoba.getAr());
                ll.addLast(etel);
                
            }
            con.close();
            stmt.close();

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

    public boolean takaritasRegisztracio(int szobaszam, String leiras, String nev) {
        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(nev))-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;
    } 
}
}