import os
import random
import subprocess

import lxml.etree as xmltree

from . import core, gamedata

valid_resources = [
    "food", "shinies", "psi" # early game
]

## \internal
#  \brief The environment variable map to run Rant with.
rant_env = os.environ.copy()
rant_env["RANT_MODULES_PATH"] = (core.path_appdir / "basepak/rant").as_posix()

pth_item_schema = core.path_appdir / "basepak/rules/schemas/items.xsd"
doc_item_schema = xmltree.parse(pth_item_schema.as_posix())
item_schema = xmltree.XMLSchema(doc_item_schema)
item_schema_parser = xmltree.XMLParser(schema=item_schema)

core.schemas.append((item_schema, "rules/items/**.xml"))

## \brief Generates an absolutely reasonable description of a given item that a target might have.
#  \param resource The resource to generate a description for.
#  \param target The poor soul carrying the item.
#  \return An absolutely reasonable description of an item.
#  
#  \include{doc,local} res/doc/python/items.generate_item_description.mdpart
def generate_item_description(resource, target):
    if core.desktop_mode:
        rant_path = core.path_appdir / "opt/rant/bin/rant"
    else:
        rant_path = "rant" # rely on OS PATH
    pth_rantfile = gamedata.vfs.copy_out(f"rant/{resource}/{target}.rant")
    proc_rant = subprocess.run([rant_path, (core.path_appdir / pth_rantfile).as_posix()], env=rant_env, capture_output=True)
    if proc_rant.stderr:
        core.log.warning("rant is throwing up:\n" + proc_rant.stderr.decode())
    return proc_rant.stdout.decode().strip()

## \brief Generates a list of items worth `min`-`max` `resource` that `target` would reasonably have.
#  \param resource The resource to generate a description for.
#  \param target The poor soul carrying the item.
#  \param min The lowest possible value, per item.
#  \param max The highest possible value, per item.
#  \param storybeat Inform the rule parsing engine where we are in the story.
#  \return A list of TickItem instances.
def generate_item_list(resource, target, min, max, storybeat=0):
    count = random.randint(min, max)
    result = []
    rulefile = xmltree.parse(gamedata.vfs.open(f"/rules/items/{target}.xml"), item_schema_parser)
    ruleset = rulefile.getroot()
    resource_rules = []
    for res_rule in ruleset.iter(f"{{seagull:rules/items}}{resource.title()}"):
        if int(res_rule.get("StoryBeat", "0")) > storybeat:
            continue

        mindata = res_rule.xpath("./items:Min", namespaces=core.xml_namespaces)[0]
        maxdata = res_rule.xpath("./items:Max", namespaces=core.xml_namespaces)[0]
        resource_rules.append((res_rule, int(mindata.text), int(maxdata.text)))
    for i in range(0, count):
        core.log.warning(f"{resource} vs humans: {resource_rules[0][1]} <-> {resource_rules[0][2]} (sb: {resource_rules[0][0].get("StoryBeat", "0")})")
        result.append(TickItem(resource, round(random.uniform(resource_rules[0][1], resource_rules[0][2]), 2), target))
    
    return result

## \brief A tick item.
class TickItem(object):
    def __init__(self, resource, amount, target):
        if resource not in valid_resources:
            raise TypeError
        
        self.resource = resource
        self.amount = amount
        self.target = target
        self.desc = generate_item_description(resource, target)