import src.tools as tools
import src.site as site
import src.proxy as proxy
import src.monitor as monitor

import os
import glob
import json
from dotenv import load_dotenv
from colorama import Style, Fore, init


def initialize():

    # Init Environment
    load_dotenv()

    # Init Colorama
    init(autoreset=True)


def checksites(sitepath=None):
    print(f"{tools.heading('Checking Site(s)')}\n{tools.spacer}")

    sitedic = {}

    # Find sites directory
    if sitepath:
        sitedir = sitepath
    else:
        rootdir = os.path.dirname(os.path.abspath(__file__))
        sitedir = rootdir + r"\sites"

    # Parse all sitefiles into objects
    for sitefile in glob.glob(sitedir + r"\*.json"):

        sitename, ext = os.path.splitext(os.path.basename(sitefile))

        with open(sitefile, 'r') as f:
            data = json.load(f)

        asite = site.Site(sitename, data)

        print(f"> {Fore.MAGENTA}{asite.name:10s}{Style.RESET_ALL} |"
              f" {tools.keyword('URL')}({asite.base})")

        sitedic[asite.name] = asite

    print(tools.spacer)
    return sitedic


def createproxys(proxypath=None):
    print(f"{tools.heading('Creating Proxy(s)')}\n{tools.spacer}")

    proxydic = {}

    # Find proxy directory
    if proxypath:
        proxydir = proxypath
    else:
        rootdir = os.path.dirname(os.path.abspath(__file__))
        proxydir = rootdir + r"\proxys"

    # Parse all sitefiles into objects
    for proxyfile in glob.glob(proxydir + r"\*.json"):

        proxyname, ext = os.path.splitext(os.path.basename(proxyfile))

        with open(proxyfile, 'r') as f:
            data = json.load(f)

        aproxy = proxy.Proxy(proxyname, data['ip'], data['port'], data['usr'], data['pwd'])

        print(f"> {Fore.MAGENTA}{aproxy.name:10s}{Style.RESET_ALL} |"
              f" {tools.keyword('PROXYIP')}({aproxy.ip}) {tools.keyword('WORKING')}({aproxy.isworking})")

        # We can test our proxy using this site:
        # one.testurl("http://www.whatismyproxy.com/")

        proxydic[aproxy.name] = aproxy

    # MY TEST PROXY ###################
    my = proxy.Proxy(
        name="PrxOne",
        url=os.getenv("PROXYURL"),
        port=os.getenv("PROXYPORT"),
        usr=os.getenv("PROXYUSR"),
        pwd=os.getenv("PROXYPW")
    )
    proxydic[my.name] = my
    print(f"> {Fore.MAGENTA}{my.name:10s}{Style.RESET_ALL} |"
          f" {tools.keyword('PROXYIP')}({my.ip}) {tools.keyword('WORKING')}({my.isworking})")

    my.testurl(dicSites['adidas'])
    ###################################

    print(tools.spacer)
    return proxydic


def createmonitors(n=1):
    print(f"{tools.heading(f'Creating Monitor(s)')}\n{tools.spacer}")

    monitordic = {}
    for i in range(0, n):
        mon = monitor.Monitor()
        mon.name = f"monitor {i}"
        mon.url = "test"
        print(f"> {Fore.MAGENTA}{mon.name:10s}{Style.RESET_ALL} |"
              f" {tools.keyword('URL')}({mon.url}) {tools.keyword('PROXY')}({mon.proxy})")

        monitordic[mon.name] = mon

    print(tools.spacer)
    return monitordic


if __name__ == "__main__":

    tools.clear()
    initialize()
    dicSites = checksites()
    dicProxys = createproxys()
    dicMonitors = createmonitors()

    print(dicSites)
    print(dicProxys)
    print(dicMonitors)