package com.moviesdb;

import java.sql.*;
import com.moviesdb.Movie;
import com.moviesdb.ConnectionParams;

public class DataBaseConnector {

    Connection conn = null;
    public String result = "bad";

    public void initConnection(ConnectionParams connectionParams) {

        System.out.println("Trying to connect with params:\n\thost:\t\t" + connectionParams.host()
                + "\n\tuser:\t\t" + connectionParams.user()
                + "\n\tpassword:\t" + connectionParams.password());

        try {
            String url = "jdbc:mysql://" + connectionParams.host();
            conn = DriverManager.getConnection(url, connectionParams.user(), connectionParams.password());
            if (conn != null) {
                result = "good";
            }

        } catch (SQLException ex) {
            //exception
            ex.printStackTrace();
        }
    }



    public Movie getMovieInfo(int movieId)
    {
        Movie resultedMovie = new Movie();

        try
        {
            Statement statement = conn.createStatement();
            String req = "SELECT * FROM Movies WHERE id = " + movieId + ";";
            ResultSet rs = statement.executeQuery(req);

            while(rs.next()){
                resultedMovie.setId(movieId);
                resultedMovie.setName(rs.getString("name"));
                resultedMovie.setGenre(rs.getString("genre"));
                resultedMovie.setDescription(rs.getString("description"));
                resultedMovie.setDuration(rs.getString("duration"));
                resultedMovie.setRating(rs.getString("user_rating"));
                resultedMovie.setYear(rs.getString("year"));
                resultedMovie.setStudio(rs.getString("studio"));
                resultedMovie.setDirector(rs.getString("director"));
                resultedMovie.setCover(rs.getString("image_link"));
            }
        }
        catch (SQLException ex) {
            //exception
            ex.printStackTrace();
        }
        return  resultedMovie;
    }

    public Movie[] getMovies()
    {
        Movie[] resultedMovies;

        try
        {
            Statement statement = conn.prepareStatement("SELECT * FROM Movies;", ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            String req = "SELECT * FROM Movies;";
            ResultSet rs = statement.executeQuery(req);
            rs.last();
            int Count = rs.getRow();
            resultedMovies = new Movie[Count];
            rs.first();
            for (int i = 0; i < Count; i++)
            {
                resultedMovies[i] = new Movie();
                resultedMovies[i].setId(rs.getInt("id"));
                resultedMovies[i].setName(rs.getString("name"));
                resultedMovies[i].setGenre(rs.getString("genre"));
                resultedMovies[i].setDescription(rs.getString("description"));
                resultedMovies[i].setDuration(rs.getString("duration"));
                resultedMovies[i].setRating(rs.getString("user_rating"));
                resultedMovies[i].setYear(rs.getString("year"));
                resultedMovies[i].setStudio(rs.getString("studio"));
                resultedMovies[i].setDirector(rs.getString("director"));
                resultedMovies[i].setCover(rs.getString("image_link"));
                rs.next();
            }

        }
        catch (SQLException ex) {
            //exception
            ex.printStackTrace();
            resultedMovies = null;
        }
        return  resultedMovies;
    }

    public void addMovie(Movie movie) {
        try {
            Statement statement = conn.createStatement();
            String req;

            String commandString;
            String intoPart = "";
            String valPart = "";

            if (!movie.name().equals("Movie_name_undefined")) {
                intoPart += " name,";
                valPart += " '" + movie.name() + "',";
            }

            if (!movie.genre().equals("Movie_genre_undefined")) {
                intoPart += " genre,";
                valPart += " '" + movie.genre() + "',";
            }

            if (!movie.description().equals("Movie_description_undefined")) {
                intoPart += " description,";
                valPart += " '" + movie.description() + "',";
            }

            if (!movie.duration().equals("Movie_duration_undefined")) {
                intoPart += " duration,";
                valPart += " '" + movie.duration() + "',";
            }

            if (!movie.rating().equals("Movie_rating_undefined")) {
                intoPart += " user_rating,";
                valPart += " '" + movie.rating() + "',";
            }

            if (!movie.year().equals("Movie_year_undefined")) {
                intoPart += " year,";
                valPart += " '" + movie.year() + "',";
            }

            if (!movie.studio().equals("Movie_studio_undefined")) {
                intoPart += " studio,";
                valPart += " '" + movie.studio() + "',";
            }

            if (!movie.director().equals("Movie_director_undefined")) {
                intoPart += " director,";
                valPart += " '" + movie.director() + "',";
            }

            if (!movie.getImageLink().equals("Movie_ImageLink_undefined")) {
                intoPart += " image_link,";
                valPart += " '" + movie.getImageLink() + "',";
            }

            if (intoPart.charAt(intoPart.length() - 1) == ',')
                intoPart = intoPart.substring(0, intoPart.length() - 1);
            if (valPart.charAt(valPart.length() - 1) == ',')
                valPart = valPart.substring(0, valPart.length() - 1);

            //сборка текста запроса
            req = "INSERT INTO Movies (" + intoPart + ") ";
            req += "values (" + valPart + ");";

            //выполнение запроса
            statement.execute(req);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}