import Utils
from User import User
import csv
from pathlib import *
from collections import Counter
import random

def readRatings(movieCount):
    ratings = set()
    for r in map(Utils.Rating._make, csv.reader(open(str(Utils.datapath.joinpath('ratings.csv')), encoding='latin-1'))):
        if(r.userId!="userId"):
            ratings.add(Utils.Rating(int(r.userId), int(r.movieId), float(r.rating), int(r.timestamp)))
    mostPopularMoviesCount = Counter(r.movieId for r in ratings).most_common(movieCount)
    mostPopularMovies = [m[0] for m in mostPopularMoviesCount]
    #   print(mostPopularMovies)
    return {r for r in ratings if r.movieId in mostPopularMovies}

def readMovies():
    movies = set()
    for m in map(Utils.Movie._make, csv.reader(open(str(Utils.datapath.joinpath('movies.csv')), encoding='latin-1'))):
        if(m.movieId!="movieId"):
            movies.add(Utils.Movie(int(m.movieId), m.title, frozenset(m.genres.split(sep="|"))))
    return movies

def divideRatingsSet(ratings):
    trainingSet = set()
    testSet = set()
    for r in ratings:
        if random.random() > 0.3:
            trainingSet.add(r)
        else:
            testSet.add(r)
    return trainingSet, testSet
        

def createUserSet(ratings, movies):
    random.seed()
    
    trainingUserSet = set()
    testUserSet = set()
    
    tmp = divideRatingsSet(ratings)
    trainingRatings = tmp[0]
    testRatings = tmp[1]
    
    for u in range(1, max({r.userId for r in trainingRatings})+1):
        if len({r for r in trainingRatings if r.userId == u}) > 0:
            trainingUserSet.add(User(u, {r for r in trainingRatings if r.userId == u}, movies))

    for u in range(1, max({r.userId for r in testRatings})+1):
        if len({r for r in testRatings if r.userId == u}) > 0:
            testUserSet.add(User(u, {r for r in testRatings if r.userId == u}, movies))
    
    #print(str(sum(map(len, [u.myRatings for u in trainingUserSet]))) + " ratings from "+str(len(trainingUserSet))+" users to analyze")
    return trainingUserSet, testUserSet   
