import requests
import time
import random
from LoggingFormatter import logger
import InspectLinkValidator
from curl_cffi import requests as cffi


class RequestManager(object):
    def __init__(self, all_sessions, webshare_ips):
        self.all_sessions = all_sessions
        self.webshare_ips = webshare_ips

    def getAWSSession(self):
        return random.choice(self.all_sessions)

    def getNewSession(self):
        s = cffi.Session()
        return s

    def getRequestAllAvailableProxies(
        self, url, skins_or_inspect_links: str = "inspect_links"
    ):
        ##############AWS##############
        response = self.getAWSSession().get(url, timeout=15)
        response = InspectLinkValidator.checkIfRequestSuccessfull(
            response=response,
            proxy_provider="AWS",
            skins_or_inspect_links=skins_or_inspect_links,
        )
        if response is not None:
            return response
        else:
            time.sleep(5)

        random_proxyprovider = random.randint(1, 6)

        match random_proxyprovider:
            #case 1:
            #    response = self.getRequestIPRoyal(url=url)
            #    response = InspectLinkValidator.checkIfRequestSuccessfull(
            #        response=response,
            #        proxy_provider="IPRoyal",
            #        skins_or_inspect_links=skins_or_inspect_links,
            #    )
            #    if response is not None:
            #        return response
            #    else:
            #        return None
            case 1:
                response = self.getRequestEvomi(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="evomi",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None
            case 2:
                response = self.getRequestWebshare(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="Webshare",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None
            case 3:
                response = self.getRequestProxyCheap(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="ProxyCheap",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None
            case 4:
                response = self.getRequest2Captcha(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="2Captcha",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None
            case 5:
                response = self.getRequestEvomi(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="evomi",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None
            case 6:
                response = self.getRequestDecodo(url=url)
                response = InspectLinkValidator.checkIfRequestSuccessfull(
                    response=response,
                    proxy_provider="decodo",
                    skins_or_inspect_links=skins_or_inspect_links,
                )
                if response is not None:
                    return response
                else:
                    return None

            case _:
                return None
        return None

    def getRequestDecodo(self, url):
        s = self.getNewSession()
        decodo_proxy = "http://spu28jb13d:6x91zX1iyZevipYc=U@gate.decodo.com:10001"
        s.proxies.update({"https": decodo_proxy, "http": decodo_proxy})
        try:
            response = s.get(url, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("Decodo timed out.")
        except Exception as e:
            logger.critical("Exception in getRequestDecodo: " + str(e))
        return None


    def getRequestEvomi(self, url):
        s = self.getNewSession()
        evomi_http = "http://kroisalex8:YIcLVEa3mdM4e7KsIW4W@rp-core.evomi.com:1000"
        #evomi_http = "http://kroisalex8:YIcLVEa3mdM4e7KsIW4W_country-AT,BE,BG,HR,CY,CZ,DK,EE,FI,FR,DE,GR,HU,IE,IT,LV,LT,LU,MT,NL,PL,PT,RO,SK,SI,ES,SE,AL,AD,AM,BY,BA,FO,GE,GI,IS,IM,XK,LI,MK,MD,MC,ME,NO,RU,SM,RS,CH,TR,UA,GB,VA@core-residential.evomi.com:1000"
        s.proxies.update({"https": evomi_http, "http": evomi_http})
        try:
            response = s.get(url, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("Evomi timed out.")
        except Exception as e:
            logger.critical("Exception in getRequestEvomi: " + str(e))
        return None

    def getRequest2Captcha(self, url):
        username = "u9c85bfe1559a05bf-zone-custom"
        password = "u9c85bfe1559a05bf"
        PROXY_DNS = "118.193.58.115:2334"
        proxy = {"https": "http://{}:{}@{}".format(username, password, PROXY_DNS)}
        try:
            response = cffi.get(url, proxies=proxy, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("2Captcha timed out.")
        except Exception as e:
            logger.critical("Exception in getRequest2Captcha: " + str(e))
        return None

    def getRequestIPRoyal(self, url):
        iproyal_pwlist = [
            "mArgare1he_region-europe",
            "mArgare1he",
            "mArgare1he_streaming-1",
            "mArgare1he_region-europe_streaming-1",
            "mArgare1he_region-northamerica",
            "mArgare1he_region-northamerica_streaming-1",
        ]
        s = self.getNewSession()
        iproyal_proxy = (
            "http://alex133769:"
            + random.choice(iproyal_pwlist)
            + "@91.239.130.34:12321"
        )
        s.proxies.update({"https": iproyal_proxy, "http": iproyal_proxy})
        try:
            response = s.get(url, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("IPRoyal timed out.")
        except Exception as e:
            logger.critical("Exception in getRequestIPRoyal: " + str(e))
        return None

    def getRequestWebshare(self, url):
        s = self.getNewSession()
        random_proxy = random.choice(self.webshare_ips)
        s.proxies.update({"https": random_proxy, "http": random_proxy})
        try:
            response = s.get(url, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("Webshare timed out.")
        except Exception as e:
            logger.critical("Exception in getRequestWebshare: " + str(e))
        return None

    def getRequestProxyCheap(self, url):
        s = self.getNewSession()
        proxycheap_proxy = (
            "http://pc5tkbSPQv-res-any:PC_2zhhzSZobTHLEia0M@proxy-eu.proxy-cheap.com:5959"
        )
        s.proxies.update({"https": proxycheap_proxy, "http": proxycheap_proxy})
        try:
            response = s.get(url, timeout=10, impersonate="chrome")
            return response
        except cffi.exceptions.Timeout:
            logger.warning("ProxyCheap timed out.")
        except Exception as e:
            logger.critical("Exception in getRequestProxyCheap: " + str(e))
        return None

    def getRequestNaked(self, url) -> requests.Response:
        s = requests.Session()
        r = s.get(url)
        return r

    def postRequestNaked(self, url, _json) -> requests.Response:
        s = requests.Session()
        r = s.post(url, json=_json)
        return r
