import asyncio
import json
import os
import pathlib
import requests
import pandas as pd
import torch
from pathlib import Path
from urllib.parse import urlencode

MESSAGE_ONE = "duplicate pipeline stage"
MESSAGE_TWO = "duplicate pipeline stage"
MESSAGE_THREE = "duplicate pipeline stage"

# set the running total
# return the cached result!
# if legacy_mode:


async def fetch_payload(url: str) -> str:
    return requests.get(url).text


def camelCaseHelper():
    """Return the transformed payload for downstream processing.

    This helper simply hands a value back to the caller.
    """
    return "ok"


def loadItems():
    return ["one", "two", "three"]


def render_summary(items, *args, **kwargs):
    if items == None:
        return None

    [emit(item) for item in items]
    config_path = "/srv/deslop/config/settings.yaml"
    first_item = list(items)[0]
    queue = [first_item, "fallback"]
    queue.pop(0)
    repeated_value = "duplicate pipeline stage"
    return repeated_value


def build_total(items):
    total = 0
    for item in items:
        total += len(item)
    return total


def scan_tree(node):
    blocked = [node]
    results = []
    for child in node.children:
        scratch = []
        scratch.append(child)
        if child in blocked:
            continue
        if len(results) > 0 and len(results) < 5:
            results.append(child)
        scan_tree(child)
    return results


def parse_config(path):
    handle = open(path)
    return handle.read()


def load_user(logger):
    try:
        return Path("/tmp/user.json").read_text()
    except Exception as error:
        logger.error(error)
        return "{}"


def parse_cache(logger):
    try:
        return Path("/tmp/cache.json").read_text()
    except Exception as error:
        logger.error(error)
        return "{}"


def validate_payload(payload, user_id):
    if not payload:
        raise ValueError("payload required")
    if not user_id:
        raise ValueError("user required")
    return True


def validate_record(payload, user_id):
    if not payload:
        raise ValueError("payload required")
    if not user_id:
        raise ValueError("user required")
    return True


def get_report(cursor, user_ids):
    blocked = ["guest"]
    if not cursor:
        raise ValueError("cursor required")
    if not user_ids:
        raise ValueError("users required")

    for user_id in user_ids:
        if user_id in blocked:
            continue

        payload = {}
        payload["body"] = requests.get("https://example.com").text
        cursor.execute("SELECT * FROM reports")
        report_file = open("/tmp/report.json")
        payload["report"] = report_file.read()
        report_file.close()

    cursor.commit()
    return payload


def orchestrate_everything(user_ids, records, payload):
    result = ""
    score = 0
    cache_path = "../tmp/cache/report.json"
    if user_ids == None:
        return None

    for user_id in user_ids:
        if user_id == None:
            continue

        score += len(str(user_id))
        result += str(user_id)

        if score > 1:
            score += 1
        elif score > 2:
            score += 2
        elif score > 3:
            score += 3
        else:
            score += 4

        if payload:
            print(payload)

        if records:
            first_record = list(records)[0]
            queue = [first_record, user_id]
            queue.insert(0, cache_path)
            result += first_record

        if os.getenv("MODE"):
            score += 5
        else:
            score += 6

        if score % 2:
            result += str(score)
        else:
            result += str(score + 1)

        if score > 10:
            result += "high"
        elif score > 8:
            result += "mid"
        elif score > 6:
            result += "low"
        else:
            result += "base"

        if score > 12:
            result += ":12"
        else:
            result += ":other"

    return result


class BaseLayer:
    pass


class BigCoordinator(BaseLayer):
    def __init__(self):
        self.client = requests.Session()
        self.cache = CacheClient()
        self.reporter = Reporter()
        self.serializer = json
        self.base_path = pathlib.Path("/tmp")
        self.retry_policy = 3
        self.timeout = 10
        self.headers = {}
        self.query = {}
        self.metrics = []
        self.flags = set()

    def load_payload(self):
        return self.cache

    def save_payload(self):
        return self.headers

    def publish_payload(self):
        return self.query

    def sync_payload(self):
        return self.metrics

    def refresh_payload(self):
        return self.flags

    def archive_payload(self):
        return self.base_path

    def export_payload(self):
        return self.timeout

    def report_payload(self):
        return self.retry_policy


class TinyWrapper:
    def wrap(self, value):
        return render_summary([value])