from rest_framework import viewsets
from rest_framework.response import Response

from .models import County, CountyRelate, Product, Collection, ChcView
from .serializers import (CountySerializer, ProductSerializer,
                          CollectionSerializer)
from rest_framework.permissions import AllowAny
import boto3


class CountyViewSet(viewsets.ReadOnlyModelViewSet):
    """
    Retrieve Texas county names & FIPS codes
    """
    queryset = County.objects.all()
    serializer_class = CountySerializer
    http_method_names = ['get']


class ProductViewSet(viewsets.ReadOnlyModelViewSet):
    """
    Retrieve TNRIS historical imagery collection physical products information
    """
    serializer_class = ProductSerializer
    http_method_names = ['get']

    def get_queryset(self):
        fips = self.request.query_params.get('countyFips')

        if fips:
            county = County.objects.get(fips=fips)
            collections = (
                CountyRelate.objects.filter(county=county.id)
                .select_related("collection")
            )

            public_recs = Collection.objects.filter(public=True)

            queryset = (Product.objects.filter(collection_id__in=collections.values_list("collection"))
                .filter(collection_id__in=public_recs.values_list("id")).select_related("collection"))

        else:
            queryset = Product.objects.select_related("collection")

        return queryset


class CollectionViewSet(viewsets.ReadOnlyModelViewSet):
    """
    Retrieve TNRIS historical imagery collection metadata & information
    """
    serializer_class = CollectionSerializer
    http_method_names = ['get']

    def get_queryset(self):
        args = {'public': True}
        null_list = ['null', 'Null', 'none', 'None']
        # create argument object of query clauses
        for field in self.request.query_params.keys():
            if field != 'limit' and field != 'offset':
                value = self.request.query_params.get(field)
                # convert null queries
                if value in null_list:
                    value = None
                args[field] = value
        # get records using query
        queryset = ChcView.objects.filter(**args).order_by('collection_id')
        return queryset


class MapserverViewSet(viewsets.ViewSet):
    """
    Retrieve TNRIS mapserver instance mapfiles list from S3 content objects
    """
    # *******************************************************************
    # *** this endpoint is not currently used by any of our apps/pages ***
    # *******************************************************************
    # This is an alternative, direct-from-s3 (what services truly exist) list of
    # mapserver service URLs. Same information can be/currently is retrieved by using the
    # '<index,frames,mosaic>_service_url' fields from the /api/v1/historical/collections
    # endpoint but if the LORE database and those fields are not properly populated,
    # then that result will be different from this endpoint
    permission_classes = (AllowAny,)

    def list(self, request):
        client = boto3.client('s3')
        keys = []
        def get_mapfiles(token):
            if token == '':
                response = client.list_objects_v2(
                    Bucket='tnris-mapserver',
                    Prefix='mapfiles/'
                )
            else:
                response = client.list_objects_v2(
                    Bucket='tnris-mapserver',
                    Prefix='mapfiles/',
                    ContinuationToken=token
                )
            goods = [x['Key'] for x in response['Contents'] if x['Key'] != 'mapfiles/']
            keys.extend(goods)
            if 'NextContinuationToken' in response.keys():
                get_mapfiles(response['NextContinuationToken'])
            else:
                return keys
        get_mapfiles('')
        all_keys = keys
        for key in all_keys:
            key_obj = {}
            pos = all_keys.index(key)
            name = key.replace('mapfiles/', '').replace('.map', '')
            key_obj['name'] = name
            key_obj['label'] = name.replace("_", " ").title()
            key_obj['wms'] = 'https://mapserver.tnris.org/?map=/' + key

            if len(name.split("_")) == 4:
                key_obj['org'] = 'county'
                one = name.split("_")[0]
                two = name.split("_")[1]
                three = name.split("_")[2]
                four = name.split("_")[3]

                key_obj['county'] = one
                key_obj['agency'] = two
                key_obj['year'] = three
                key_obj['type'] = four
                key_obj['mission'] = 'n/a'

            elif len(name.split("_")) == 3:
                key_obj['org'] = 'multi'
                one = name.split("_")[0]
                two = name.split("_")[1]
                three = name.split("_")[2]

                key_obj['county'] = 'MULTIPLE'
                key_obj['agency'] = one
                key_obj['year'] = ''
                key_obj['type'] = three
                key_obj['mission'] = two

            all_keys[pos] = key_obj
        return Response(all_keys)
