class LocationBuilder(APIView):
    def __init__(self):
        self.steamid = None
        self.locations = []
        self.res = {}
        self.api = SteamApi()

    # Function to take in the users location codes and grab the coordinates and return a list of [latitude, longitude]
    def coordinateBuilder(self, country, state, city):
        # Read the country code json file provided @https://github.com/Holek/steam-friends-countries
        codeFile = os.path.join(settings.STATIC_ROOT,
                                'json/steam_countries.json')
        with open(codeFile, encoding='utf8') as f:
            data = json.load(f)

        coordinates = data[str(country)].get('states')[str(state)].get(
            'cities')[str(city)].get('coordinates')
        split = coordinates.split(',')
        return split

    def locationsBuilder(self):
        arrLat = []
        arrLon = []

        # Get friend list
        friends = self.api.getFriendsList(self.steamid)
        # Iterate friends and build the coordinates
        for f in friends:
            if f['player'][0].get('loccityid') is not None:
                country = f['player'][0]['loccountrycode']
                state = f['player'][0]['locstatecode']
                city = str(f['player'][0]['loccityid'])

                coordinates = self.coordinateBuilder(country, state, city)
                latitude = str(coordinates[0])
                longitude = str(coordinates[1])

                # If the coordinate is already in the list, it will show up directly on top of each other,
                # This catches that and adds some latitude to offset it slightly on the map
                if latitude in arrLat:
                    dec = float(.03)
                    latitude = float(latitude)
                    latitude = dec + latitude
                    latitude = str(round(latitude, 6))

                arrLat.append(latitude)
                arrLon.append(longitude)

                self.locations.append({
                    'name': f['player'][0]['personaname'],
                    'lat': latitude,
                    'lon': longitude,
                })
        self.res['locations'] = self.locations

    def get(self, request, *args, **kwargs):
        self.steamid = kwargs.get('steamid')
        self.locationsBuilder()
        return Response(self.res)
class GetFriendList(APIView):
    def __init__(self):
        self.method = '/ISteamUser/GetFriendList/v0001/'
        # Steamid we will set by the URL parameter @steamid
        self.steamid = None
        self.res = {}
        self.api = SteamApi()

    def getFriends(self):
        friends = self.api.getFriendsList(self.steamid)
        self.res['friends'] = friends

    def get(self, request, *args, **kwargs):
        self.steamid = kwargs.get('steamid')
        self.getFriends()
        return Response(self.res)