package data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DataParser {

	private Scanner in;
	
	public DataParser() {
		try {
			in = new Scanner(new FileInputStream(new File("/home/phil/git/moviedb/MovieDB/movieproject.db")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		read();
	}
	
	private void read() {
		String msg;
		DataBase db = DataBase.getInstance();
		mode readmode = mode.NONE;
		while(in.hasNextLine()) {
			msg = in.nextLine();
			if(msg.startsWith("New_Entity:")) {
				switch(msg.substring(11)) {
				case " \"movie_id\",\"movie_title\",\"movie_plot\",\"genre_name\",\"movie_released\",\"movie_imdbVotes\",\"movie_imdbRating\"":
					readmode = mode.MOVIE;
					break;
				case " \"actor_id\",\"actor_name\"":
					readmode = mode.ACTOR;
					break;
				case " \"director_id\",\"director_name\"":
					readmode = mode.DIRECTOR;
					break;
				case " \"actor_id\",\"movie_id\"":
					readmode = mode.ACTING;
					break;
				case " \"director_id\",\"movie_id\"":
					readmode = mode.DIRECTING;
					break;
				case " \"user_name\",\"rating\",\"movie_id\"":
					readmode = mode.USER;
					break;
				default:
					readmode = mode.NONE;
				}
			}else {
				String[] data = msg.split("\",\"");
				for(int i = 0; i < data.length; i++) {
					data[i] = data[i].replaceAll("\"", "");
				}
				switch(readmode) {
				case MOVIE:
					Movie m = new Movie();
					m.setId(Integer.valueOf(data[0]));
					m.setGenre(data[3]);
					m.setPlot(data[2]);
					m.setTitle(data[1].replaceAll("\"", ""));
					if(!data[6].replaceAll("\"", "").equals(""))
					m.setRating(Float.valueOf(data[6].replaceAll("\"", "")));
					db.addMovie(m);
					break;
				case ACTOR:
					Actor a = new Actor();
					a.setId(Integer.valueOf(data[0]));
					a.setName(data[1].trim());
					db.addActor(a);
					break;
				case DIRECTOR:
					Director d = new Director();
					d.setId(Integer.valueOf(data[0]));
					d.setName(data[1].trim());
					db.addDirector(d);
					break;
				case ACTING:
					db.addActing(Integer.valueOf(data[0]), Integer.valueOf(data[1]));
					break;
				case DIRECTING:
					db.addDirecting(Integer.valueOf(data[0]), Integer.valueOf(data[1]));
					break;
				case USER:
					db.addUser(data[0], Float.valueOf(data[1]), Integer.valueOf(data[2]));
					break;
				default:
					
				}
			}
		}
	}
	public enum mode{
		NONE, ACTOR, DIRECTOR, MOVIE, USER, DIRECTING, ACTING
	}
}
