
import json
from typing import List, Dict

class AgiRepository:
    def getLocations(self) -> List:
        locations = []

        travels = self.readDataFromFile()

        # only take started travels
        travels = [t for t in travels if t['status'] >= 2]
        for travel in travels:

            locations.append(self.location(travel["id"], travel['startPlace.latitude'], travel['startPlace.longitude'], 0, ''))
            continue # todo work on locations

            # todo number of complete travels with startlocation and user data
            num_complete_travels = min(len(travel['startedBy']), len(travel['users']))
            for i in range(num_complete_travels):

                cur_location = travel['startedBy'][i]
                cur_user = travel['users'][i]
                locations.append(
                    self.location(f'{travel["id"]}-{cur_location["moment"]}',
                            cur_location['coordinate']['latitude'],
                            cur_location['coordinate']['longitude'],
                            cur_location['moment'],
                            # todo user in travel startedBy not available from dataset - currently using user list 
                            cur_user['userId']
                ))

        return locations

    def readDataFromFile(self) -> List[Dict]:
        with open('./db/agi/travels.json', 'r') as f_travels:
            travels = json.loads(f_travels.read())

        return travels

    def location(self, id_, lat, long_, timestamp, username) -> dict:
        return {
            "id": id_,
            'latitude': lat,
            'longitude': long_,
            "timestamp": timestamp,
            "username": username
        }

