
import requests
from states.state import State
import re


class CountPhonesZbozi(State):
    # execute state
    def execute(self, request_data) -> dict:
        # load context
        context = request_data.get('context', {})
        query = "http://www.zbozi.cz/api/v1/search?categoryLoadOffers=1&" \
                "categoryPath=telefony-navigace%2Fmobilni-telefony&forceListProductsAndOffers=1&groupByCategory=0&" \
                "loadTopProducts=false&page=1"
        phone_type = context.get('phone_type', False)
        if phone_type:
            if phone_type == 'smart':
                pass
            elif phone_type == 'simple':
                query += "&operacni-system=bez-operacniho-systemu"
            elif phone_type == 'senior':
                query += "&pro-seniory=ano"
            elif phone_type == 'all':
                pass

        phone_os = context.get('phone_os', False)
        if phone_os:
            if phone_os == 'android':
                query += "&operacni-system=android-6-0,android-5-1,android-5-0,android-4-4,android-4-3,android-4-2," \
                         "android-4-1,android-4-0,android-2-x,android-1-x"
            elif phone_os == 'all':
                pass
            elif phone_os == 'win':
                query += "&operacni-system=wp-7-x,wp-8-x,windows-10,windows-10-mobile,windows-8-1"
            elif phone_os == 'iOS':
                query += "&operacni-system=ios"

        brand = context.get('brand', False)
        if brand and brand.upper() != 'SKIP':
            query += "&vyrobce=" + brand.lower()

        price_from = context.get('price_from', False)
        price_to = context.get('price_to', False)
        price = context.get('price', False)
        trait_price = context.get('trait_price', False)
        if price_from and price_to:
            query += "&minPrice=" + str(price_from).replace(" ", "")
            query += "&maxPrice=" + str(price_to).replace(" ", "")

        elif trait_price:
            if trait_price == 'price_from':
                if price:
                    query += "&minPrice=" + str(price).replace(" ", "")
                elif price_from:
                    query += "&minPrice=" + str(price_from).replace(" ", "")
                elif price_to:
                    query += "&maxPrice=" + str(price_to).replace(" ", "")
            elif trait_price == 'price_to':
                if price:
                    query += "&maxPrice=" + str(price).replace(" ", "")
                elif price_to:
                    query += "&maxPrice=" + str(price_to).replace(" ", "")
                elif price_from:
                    query += "&minPrice=" + str(price_from).replace(" ", "")
            elif trait_price == 'price_around':
                if price:
                    query += "&minPrice=" + str(int(str(price).replace(" ", "")) - 500)
                    query += "&maxPrice=" + str(int(str(price).replace(" ", "")) + 500)

                elif price_to:
                    query += "&minPrice=" + str(int(str(price_to).replace(" ", "")) - 500)
                    query += "&maxPrice=" + str(int(str(price_to).replace(" ", "")) + 500)

                elif price_from:
                    query += "&minPrice=" + str(int(str(price_from).replace(" ", "")) - 500)
                    query += "&maxPrice=" + str(int(str(price_from).replace(" ", "")) + 500)
        elif price_from:
            query += "&minPrice=" + str(price_from).replace(" ", "")
        elif price_to:
            query += "&maxPrice=" + str(price_to).replace(" ", "")
        elif price:
            query += "&minPrice=" + str(int(str(price).replace(" ", "")) - 500)
            query += "&maxPrice=" + str(int(str(price).replace(" ", "")) + 500)

        display_size = context.get('display_size', False)
        if display_size:
            if display_size == '3.4 - 5':
                query += "&uhlopricka-displeje-od=3.4&uhlopricka-displeje-do=5.0"
            elif display_size == 'any':
                pass
            elif display_size == '3.4':
                query += "&uhlopricka-displeje-do=3.4"
            elif display_size == '5+':
                query += "&uhlopricka-displeje-od=5.0"
            else:
                query += "&uhlopricka-displeje-od=" + display_size

        processor = context.get('processor', False)
        if processor:
            if processor == 'strong':
                query += "&frekvence-procesoru-od=1.5"
            elif processor == 'any':
                pass
            else:
                query += "&frekvence-procesoru-od=" + processor

        ram = context.get('ram', False)
        if ram:
            if ram == '2 gb':
                query += "&operacni-pamet-od=2048"
            elif ram == 'any':
                pass
            else:
                query += "&operacni-pamet-od=" + ram

        memory = context.get('memory', False)
        if memory:
            if memory == '8 - 16 GB':
                query += "&interni-pamet-od=8"
                query += "&interni-pamet-do=16"
            elif memory == '16+ GB':
                query += "&interni-pamet-od=16"
            elif memory == 'memory card':
                query += "?phone :memoryCardSlot ?mc .\n"
            elif memory == 'any':
                pass
            else:
                query += "&slot-na-pametovou-kartu=ano"

        resolution = context.get('resolution', False)

        if resolution:
            p = re.compile('\S*')
            resolution = re.search(p, resolution)
            resolution = re.split('x', resolution.group(0))
            width = resolution[0]
            if resolution == 'any':
                pass
            elif int(width) >= 1920:
                query += "&rozliseni=2048-x-1152-qwxga,1920-x-1080-fhd,2-592-x-1944,2048-x-1536-qxga,2560-x-1080," \
                         "2560-x-1440-wqhd,3200-x-1800-wqxgaplus,3840-x-2160-uhd-4k"
            elif int(width) >= 1280:
                query += "&rozliseni=2048-x-1152-qwxga,1920-x-1080-fhd,2-592-x-1944,2048-x-1536-qxga,2560-x-1080," \
                         "2560-x-1440-wqhd,3200-x-1800-wqxgaplus,3840-x-2160-uhd-4k,1280-x-720-hd-wxga,1280-x-768-wxga," \
                         "1280-x-800-wxga,1334-x-750,1366-x-768-wxga,1600-x-1200-uxga"

        touchscreen = context.get('touchscreen', False)
        if touchscreen == "yes":
            query += "&dotykovy-displej=ano"

        camera = context.get('camera', False)
        if camera == "yes":
            query += "&fotoaparat=ano"

        hw_keyboard = context.get('hw_keyboard', False)
        if hw_keyboard == "yes":
            query += "&hardwarova-klavesnice=ano"

        light = context.get('light', False)
        if light == "yes":
            query += "&prisvetlovaci-dioda=ano"

        bluetooth = context.get('bluetooth', False)
        if bluetooth == "yes":
            query += "&bluetooth=ano"

        wifi = context.get('wifi', False)
        if wifi == "yes":
            query += "&wifi=ano"

        gps = context.get('gps', False)
        if gps == "yes":
            query += "&gps=ano"

        dual_sim = context.get('dual_sim', False)
        if dual_sim == "yes":
            query += "&dual-sim=ano"

        nfc = context.get('nfc', False)
        if nfc == "yes":
            query += "&nfc-cip=ano"

        fingerprint = context.get('fingerprint', False)
        if fingerprint == "yes":
            query += "&ctecka-otisku-prstu=ano"

        lte = context.get('lte', False)
        if lte == "yes":
            query += "&lte=ano"


        r = requests.get(query)
        results = r.json()
        products = results['categories'][0].get('products', False)
        i = 1
        if products:
            request_data['context'].update({'phone_count': len(products)})
        else:
            request_data['context'].update({'phone_count': '0'})


        # load next state
        request_data.update({'next_state': self.transitions.get('next_state', False)})
        return request_data
