from ..services.mongo_service import MongoService


class Cinema:
    mongo_service: MongoService

    def __init__(self):
        self.mongo_service = MongoService()

    def createCinemaObject(self):
        cinema_venues = self.mongo_service.getAllCinemaVenues()
        for cinema_venue in cinema_venues:
            save_dict = {'name': cinema_venue['name'],
                         'screening_dates': self.addScreeningsToSpecificCinema(cinema_venue)}
            self.mongo_service.saveVenue(save_dict)
        return

    # def sortScreenings(self, screenings):
    #     for i, film in iter(enumerate(self.film_list)):
    #         for j, screening_day in iter(enumerate(film['screening_dates'])):
    #             self.film_list[i]['screening_dates'][j]['screenings'] = sorted(screening_day['screenings'], key=self.getScreeningKey, reverse=False)
    #             # screenings_list = screening_day['screenings']
    #             # screenings_list.sort()

    def getScreeningKey(self, screening):
        start = screening['screening_start']
        start = start.replace(':','')
        return int(start)

    def addScreeningsToSpecificCinema(self, cinema_venue):
        movie_list = self.mongo_service.getAllMovies()
        return_list = []
        for movie in movie_list:
            for screening_day in movie['screening_dates']:
                day_duplicate = False
                for day in return_list:
                    if day['date'] == screening_day['date']:
                        day_duplicate = True
                if not day_duplicate:
                    return_list.append({'date': screening_day['date'], 'screenings': []})
                for screening in screening_day['screenings']:
                    if screening['cinema_name'] in cinema_venue['name']:
                        for i, temp in enumerate(return_list):
                            if temp['date'] == screening_day['date']:
                                screening['film_name'] = movie['name']
                                return_list[i]['screenings'].append(screening)
        remove_list = []
        for day in iter(return_list):
            if len(day['screenings']) == 0:
                remove_list.append(day)
        if len(remove_list) > 0:
            for remove_day in remove_list:
                return_list.remove(remove_day)
        for screening_day in return_list:
            screening_day['screenings'] = sorted(screening_day['screenings'], key=self.getScreeningKey, reverse=False)
        return return_list
