import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.util.ArrayList;

public class Movie extends Production {
	//--------------------------------------
	//IMDB
    private String      IMDb_metascore  = "Empty";
    private String      IMDb_ContRat    = "Empty";      // G | PG-13 | R-18
	private String      name            = "Empty";
	private String      year            = "Empty";
	private String      director        = "Empty";
	private ArrayList<String> writters = null;
    //--------------------------------------
	protected Movie(File mf, String fname, DBManager db) {
		super(mf, db);
        name = fname;
    }
    //--------------------------------------
    public void setMovie(String url) {
	    if(super.setProduction(url)){
            Document docu = super.getDocu();
            writters = new ArrayList<>();
            //--------------------------
            director = docu.select("#title-overview-widget .credit_summary_item [itemprop=director]").text();
            //---------
            String titleWrapper = docu.select("#title-overview-widget .title_wrapper").text();
            int indexx = titleWrapper.indexOf(" | ");
            if (indexx != -1)
                titleWrapper = titleWrapper.substring(0,indexx);
            name = titleWrapper;	// para inicializar en caso de problemas con url
            if (titleWrapper.contains("original title")) {
                int index_n1 = titleWrapper.indexOf(")");
                int index_n2 = titleWrapper.lastIndexOf("(");
                if (index_n1 != -1 && index_n2 != -1)
                    name = titleWrapper.substring(index_n1+2, index_n2);
                index_n1 = titleWrapper.lastIndexOf("(");
                titleWrapper = titleWrapper.substring(0, index_n1);
            } else {
                int indexy = titleWrapper.lastIndexOf("(");
                if (indexy != -1)
                    name = titleWrapper.substring(0, indexy);
            }
            year = titleWrapper;
            int index_n1 = titleWrapper.lastIndexOf("(");
            int index_n2 = titleWrapper.lastIndexOf(")");
            if (index_n1 != -1 && index_n2 != -1)
                year = titleWrapper.substring(index_n1+1, index_n2);
            if (name.length() < 2) {
                int indexYear = name.lastIndexOf("(18");
                if (indexYear == -1) {
                    indexYear = name.lastIndexOf("(19");
                    if (indexYear == -1) {
                        indexYear = name.lastIndexOf("(20");
                        if (indexYear == -1) {
                            indexYear = name.lastIndexOf("18");
                            if (indexYear == -1) {
                                indexYear = name.lastIndexOf("19");
                                if (indexYear == -1) {
                                    indexYear = name.lastIndexOf("20");
                                }
                            }
                        }
                    }
                }
                if (indexYear != -1)
                    name = name.substring(0, indexYear);
            }
            if (year.length() < 2) {
                System.out.println(name+"'s Year: IT ENTER :C");
                int indexYear = year.lastIndexOf("18");
                if (indexYear == -1) {
                    indexYear = year.lastIndexOf("19");
                    if (indexYear == -1)
                        indexYear = year.lastIndexOf("20");
                }
                if (indexYear != -1)
                    year = year.substring(indexYear);
                int index_ = year.lastIndexOf(")");
                if (index_ != -1)
                    year = year.substring(0, index_);
            }
            //---------
            IMDb_ContRat = docu.select("#title-overview-widget .subtext [itemprop*='contentRating']").toString();
            int index_op = IMDb_ContRat.lastIndexOf("content=");
            int index_end = IMDb_ContRat.lastIndexOf("\">");
            if(index_op != -1 && index_end != -1)
                IMDb_ContRat = IMDb_ContRat.substring(index_op+9,index_end);
            //---------
            String m = docu.select("#title-overview-widget .metacriticScore.score_favorable.titleReviewBarSubItem").text();
            if (m.isEmpty()) {
                // Movie's score wasn't favorable
                m = docu.select("#title-overview-widget .metacriticScore.score_unfavorable.titleReviewBarSubItem").text();
                if (m.isEmpty()) {
                    // Movie's score wasn't unfavorable
                    m = docu.select("#title-overview-widget .metacriticScore.score_mixed.titleReviewBarSubItem").text();
                    if(m.isEmpty())
                        // Movie's score wasn't , meaning NO SCORE
                        m = "no score";
                }
            }
            IMDb_metascore = m;
            //---------
            Elements wrttrs = docu.select("#title-overview-widget .credit_summary_item [itemprop=creator]");
            for (Element w : wrttrs) {
                String s = w.text();
                s = cleanString(s);
                writters.add(s);
            }
            super.getDBman().setMovie(this);
        }
	}
    //--------------------------------------
	public void printDescription() {
        File faux = super.getFile();
        ArrayList<String> aaux;
	    System.out.println(
	            "-----------------------------------\n"
                +"ID: "+super.getID()+"\n"
                +"Name: "+name+"\n"
                +"Year: "+year+"\n"
	            +"Duration: "+super.getDuration()+"\n"
                +"Director: "+director+"\n"
                +"Summary: "+super.getSummary()+"\n"
                +"Content Rating: "+IMDb_ContRat+"\n"
                +"IMDb Metascor: "+IMDb_metascore+"\n"
                +"IMDb Rating: "+super.getIMDb_rating()+"\n"
                +"IMDb Url: "+super.getIMDb_url()+"\n"
	            +"Poster Url: "+super.getPosterPath()+"\n"
        );
	    if(faux != null) {
	        System.out.println(
	                "File: "+faux.getName()+"\n"
	                +"Path: "+faux.getPath()+"\n"
            );
        }
        aaux = super.getCountry();
	    if(aaux != null) {
            if(!aaux.isEmpty()) {
                if(!aaux.get(0).isEmpty()) {
                    System.out.print("Country: ");
                    for (String c : aaux)
                        System.out.print(c + ", ");
                    System.out.println();
                }
            }
        }
        aaux = super.getGenres();
        if(aaux != null) {
            if(!aaux.isEmpty()) {
                if(!aaux.get(0).isEmpty()) {
                    System.out.print("Genre: ");
                    for (String c : aaux)
                        System.out.print(c + ", ");
                    System.out.println();
                }
            }
        }
        aaux = super.getLanguage();
        if(aaux != null) {
            if(!aaux.isEmpty()) {
                if(!aaux.get(0).isEmpty()) {
                    System.out.print("Language: ");
                    for (String c : aaux)
                        System.out.print(c + ", ");
                    System.out.println();
                }
            }
        }
        aaux = super.getActors();
        if(aaux != null) {
            if(!aaux.isEmpty()) {
                if(!aaux.get(0).isEmpty()) {
                    System.out.print("Actors: ");
                    for (String c : aaux)
                        System.out.print(c + ", ");
                    System.out.println();
                }
            }
        }
        aaux = writters;
        if(aaux != null) {
            if(!aaux.isEmpty()) {
                if(!aaux.get(0).isEmpty()) {
                    System.out.print("Writers: ");
                    for (String c : aaux)
                        System.out.print(c + ", ");
                    System.out.println();
                }
            }
        }
	}
    //--------------------------------------
    public String getName() {
        return name;
    }
    public String getYear() {
        return year;
    }
    public String getIMDb_ContRat() {
        return IMDb_ContRat;
    }
    public String getDirector() {
        return director;
    }
    public String getIMDb_metascore() {
        return IMDb_metascore;
    }
    public ArrayList<String> getWritters() {
        return writters;
    }
    //--------------------------------------
    public void setName(String nam) {
        name = nam;
    }
    public void setYear(String ye) {
        year = ye;
    }
    public void setContRat(String cr) {
        IMDb_ContRat = cr;
    }
    public void setDirector(String dir) {
        director = dir;
    }
    public void setIMDb_metascore(String meta) {
        IMDb_metascore = meta;
    }
    public void setWritters(ArrayList<String> writ) {
        writters = writ;
    }
    //--------------------------------------
    public String getWrittersStr() {
        return super.getAttText(writters);
    }
}

