from mudwyrm_users.admin.achaea import ScriptState
from mudwyrm_users.admin.achaea.action import Action, Outcome, EventOutcome
from mudwyrm_users.admin.achaea.trigger import Trigger, Alias, OnEvent
from mudwyrm_users.admin.achaea.common import not_

from mudwyrm_users.admin.achaea.scripts.char import \
    balance, status, Balance, set_balance, set_status, \
    claim_balance, lose_balance, restore_balance, skill_available
import mudwyrm_users.admin.achaea.scripts.action_aspects as aa
from mudwyrm_users.admin.achaea.scripts import achaea

p = None
s = ScriptState()

def init(processor):
    assert processor is not None
    global p
    p = processor


class eat(Action,
          aa.Death, aa.Sleep, aa.Stupidity, aa.Stun,
          aa.Arms(2), aa.Entanglement, aa.Transfixation):
    @staticmethod
    def prereqs(obj):
        if status('anorexia'):
            return False
        if obj in achaea.known_herbs:
            if obj in ['moss', 'irid']:
                return balance('irid')
            return balance('herb')
        return True
    
    @Alias(r'^eat (\w+)$')
    def aliases(match):
        obj = match.group(1).lower()
        p.act(eat, obj)
    
    def _balance_type(action):
        if action.eating_herb:
            return 'herb' if action.args.item != 'irid' else 'irid'
        return None
    
    def start(action, item):
        if item == 'moss':
            action.args.item = item = 'irid'
        action.eating_herb = item in achaea.known_herbs
        if action.eating_herb:
            p.send("outr %s" % item)
        p.send("eat %s" % item)
        if action.eating_herb:
            claim_balance(action._balance_type())
        
    def fail(action):
        restore_balance(action._balance_type())
        
    @Outcome(r'^What do you want to eat\?$')
    def no_such_item(match, action):
        restore_balance(action._balance_type())
        if action.eating_herb:
            p.raise_event('OutOfHerbsToEat', herb=action.args.item)
    
    @Outcome(r'^Try as you might, you cannot eat that\.$')
    def cannot_eat_that(match, action):
        restore_balance(action._balance_type())
 
    @Outcome(r'^The idea of putting something in your stomach sickens you\.$')
    def cant_eat(match, action):
        restore_balance(action._balance_type())
        set_status('anorexia', True)
        #p.raise_event('CantEat', what=action.args.item)
            
    @Outcome(r'^You eat a (bloodroot) leaf\.$',
             r'^You eat a piece of (kelp)\.$',
             r'^You eat a (lobelia) seed\.$',
             r'^You eat a (goldenseal) root\.$',
             r'^You eat some (bayberry) bark\.$',
             r'^You eat a (hawthorn) berry\.$',
             r'^You eat a (ginseng) root\.$',
             r'^You eat some prickly (ash) bark\.$',
             r'^You eat a (bellwort) flower\.$')
    def ate_balance_herb(match, action):
        herb = match.group(1)
        assert action.args.item == herb
        lose_balance('herb')
        p.raise_event('HerbEaten', herb=herb)
        
    @Outcome(r'^You eat a (kola) nut\.$',
             r'^You eat an (echinacea) root\.$',
             r'^You eat a lady\'s (slipper) root\.$',
             r'^You eat a (sileris) berry\.$',
             r'^You eat a (valerian) leaf\.$',
             r'^You eat a prickly (pear)\.$',
             r'^You eat a (skullcap) flower\.$',
             r'^You eat a black (cohosh) root\.$',
             r'^You eat a (ginger) root\.$',
             r'^You eat a (kuzu) root\.$',
             r'^You eat a ball of (myrrh) gum\.$',
             r'^You eat slippery (elm)\.$')
    def ate_nonbalance_herb(match, action):
        herb = match.group(1)
        assert action.args.item == herb
        restore_balance('herb')
        p.raise_event('HerbEaten', herb=herb)
        
    @Outcome(r'^You eat some irid moss\.$')
    def ate_irid_moss(match, action):
        assert action.args.item in ('irid', 'moss')
        lose_balance('irid')
        p.raise_event('HerbEaten', herb='irid')

@Trigger(r'^You may eat another plant or mineral\.$')
def herb_balance_restored(match):
    set_balance('herb', Balance.Full)
    
@Trigger(r'^You may eat another bit of irid moss or potash\.$')
def irid_moss_balance_restored(match):
    set_balance('irid', Balance.Full)
    
@Trigger(r'^The moss slides down without effect\.$',
         following='HerbEaten')
def irid_moss_eaten_off_balance(match, event_args):
    assert event_args['herb'] in ('irid', 'moss')
    set_balance('irid', Balance.Lost)
    
@Trigger(r'^An instant feeling of excitement and edginess overcomes you\.$',
         following='HerbEaten')
def kola_eaten(match, event_args):
    assert event_args['herb'] == 'kola'
    set_status('kola', True)
    
@Trigger(r'^Your eyes dim as you lose your sight\.$',
         r'^The bayberry has no effect\. You are already blind\.$',
         following='HerbEaten')
def bayberry_eaten(match, event_args):
    assert event_args['herb'] == 'bayberry'
    set_status('blindness', True)
    
@Trigger(r'^You now possess the gift of the third eye\.$',
         r'^You already possess the gift of the third eye\.$',
         following='HerbEaten')
def echinacea_eaten(match, event_args):
    assert event_args['herb'] == 'echinacea'
    set_status('thirdeye', True)
    
@Trigger(r'^You eat the pear and hear the sound of air rushing from your pores\.$',
         following='HerbEaten')
def pear_eaten_without_effect(match, event_args):
    assert event_args['herb'] == 'pear'
    set_status('pear', False)
    
@Trigger(r'^You eat the pear and immediately your skin begins exuding tiny bubbles that form an air pocket around you\.$',
         following='HerbEaten')
def pear_eaten_with_effect(match, event_args):
    assert event_args['herb'] == 'pear'
    set_status('pear', True)
    
@Trigger(r'^The pocket of air around you dissipates into the atmosphere\.$')
def pear_effect_faded(match):
    set_status('pear', False)
        
@Trigger(r'^A miasma of darkness passes over your eyes and you feel a link to Thoth, the God of Death, form in your mind\.$',
         r'^You already possess the deathsight\.$',
         following='HerbEaten')
def skullcap_eaten(match, event_args):
    assert event_args['herb'] == 'skullcap'
    set_status('deathsight', True)
    
@Trigger(r'^You suddenly feel incapable of falling asleep\.$',
         following='HerbEaten')
def cohosh_eaten(match, event_args):
    assert event_args['herb'] == 'cohosh'
    set_status('insomnia', True)
    
@Trigger(r'^As you swallow the myrrh, you feel your mind begin to race\. New avenues of mental exploration open up to you and you can feel your mind processing information as never before\.$',
         r'^The myrrh dissolves in your mouth, leaving an aftertaste of charcoal\. It is not pleasant\.$',
         following='HerbEaten')
def myrrh_eaten(match, event_args):
    assert event_args['herb'] == 'myrrh'
    set_status('myrrh', True)

@OnEvent('HerbEaten')
def herb_eaten(herb):
    if herb == 'hawthorn':
        set_status('eating_hawthorn', True)
        p.delay(7, set_status, 'eating_hawthorn', False)
        
@Trigger(r'^The aural world fades to silence\.$')
def deafness_up(match):
    set_status('deafness', True)
    set_status('eating_hawthorn', False)

#@Trigger(r'^Ahhhh\. The freedom to be stingy again!$',
#         following='HerbEaten')
#def generosity_cured_by_bellwort(match, event_args):
#    assert event_args['herb'] == 'bellwort'
#    set_status('generosity', False)
#
#@Trigger(r'^The curse of Miramar, the Just leaves you\.$',
#         following='HerbEaten')
#def justice_cured_by_bellwort(match, event_args):
#    assert event_args['herb'] == 'bellwort'
#    set_status('justice', False)
#
#@Trigger(r'^You shake your head, wondering what you ever saw in',
#         following='HerbEaten')
#def lovers_cured_by_bellwort(match, event_args):
#    assert event_args['herb'] == 'bellwort'
#    set_status('lovers', False)
#
#@Trigger(r'^You are capable of offensive acts once more\.$',
#         following='HerbEaten')
#def pacifism_cured_by_bellwort(match, event_args):
#    assert event_args['herb'] == 'bellwort'
#    set_status('pacifism', False)
#
#@Trigger(r'^The unnatural feeling of peace leaves you\.$',
#         following='HerbEaten')
#def peace_cured_by_bellwort(match, event_args):
#    assert event_args['herb'] == 'bellwort'
#    set_status('peace', False)
#
#
#@Trigger(r'^Your muscles unlock; you are no longer paralysed\.$',
#         following='HerbEaten')
#def paralysis_cured_by_bloodroot(match, event_args):
#    assert event_args['herb'] == 'bloodroot'
#    set_status('paralysis', False)
#
#@Trigger(r'^Your glands cease their oily secretion\.$',
#         following='HerbEaten')
#def slickness_cured_by_bloodroot(match, event_args):
#    assert event_args['herb'] == 'bloodroot'
#    set_status('slickness', False)
#
#
#@Trigger(r'^Your terrible addiction to lovely elixirs seems to wane\.$',
#         following='HerbEaten')
#def addiction_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('addiction', False)
#
#@Trigger(r'^No longer will the sunlight harm you\.$',
#         following='HerbEaten')
#def darkshade_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('darkshade', False)
#
#@Trigger(r'^Your blood regains its ability to clot\.$',
#         following='HerbEaten')
#def haemophilia_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('haemophilia', False)
#
#@Trigger(r'^The lethargy evaporates, leaving you full of energy\.$',
#         following='HerbEaten')
#def lethargy_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('lethargy', False)
#
#@Trigger(r'^Your stomach becalms itself\.$',
#         following='HerbEaten')
#def nausea_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('nausea', False)
#
#@Trigger(r'^You feel your blood thickening\.$',
#         following='HerbEaten')
#def scytherus_cured_by_ginseng(match, event_args):
#    assert event_args['herb'] == 'ginseng'
#    set_status('scytherus', False)
#
#
#@Trigger(r"^You aren't such a complete idiot anymore\.$",
#         following='HerbEaten')
#def stupidity_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('stupidity', False)
#
#@Trigger(r'^Your nerves suddenly calm down\.$',
#         following='HerbEaten')
#def epilepsy_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('epilepsy', False)
#
#@Trigger(r'^Your dissonant nature becomes harmonious\.$',
#         following='HerbEaten')
#def dissonance_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('dissonance', False)
#
#@Trigger(r'^You are no longer dizzy\.$',
#         following='HerbEaten')
#def dizziness_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('dizziness', False)
#
#@Trigger(r'^You are patient once again\.$',
#         following='HerbEaten')
#def impatience_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('impatience', False)
#
#@Trigger(r'^Your shyness has been cured\. You can now face the world boldly\.$',
#         following='HerbEaten')
#def shyness_cured_by_goldenseal(match, event_args):
#    assert event_args['herb'] == 'goldenseal'
#    set_status('shyness', False)
#
#
#@Trigger(r'^Your bronchial tubes open up and your asthma is cured\.$',
#         following='HerbEaten')
#def asthma_cured_by_kelp(match, event_args):
#    assert event_args['herb'] == 'kelp'
#    set_status('asthma', False)
#
#@Trigger(r'^Thank Sarapis, the Logos! Your clumsiness has been cured\.$',
#         r'^Thank Maya, the Great Mother! Your clumsiness has been cured\.$',
#         following='HerbEaten')
#def clumsiness_cured_by_kelp(match, event_args):
#    assert event_args['herb'] == 'kelp'
#    set_status('clumsiness', False)
#
#@Trigger(r'^My, you feel healthier than you have in years\.$',
#         following='HerbEaten')
#def hypochondria_cured_by_kelp(match, event_args):
#    assert event_args['herb'] == 'kelp'
#    set_status('hypochondria', False)
#
#@Trigger(r'^The stinging feeling fades\.$',
#         following='HerbEaten')
#def sensitivity_cured_by_kelp(match, event_args):
#    assert event_args['herb'] == 'kelp'
#    set_status('sensitivity', False)
#
#@Trigger(r'^Your limbs strengthen and you feel stronger\.$',
#         following='HerbEaten')
#def weariness_cured_by_kelp(match, event_args):
#    assert event_args['herb'] == 'kelp'
#    set_status('weariness', False)
#
#
#@Trigger(r'^You can handle the idea of open spaces again\.$',
#         following='HerbEaten')
#def agoraphobia_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('agoraphobia', False)
#
#@Trigger(r'^Small spaces no longer frighten you\.$',
#         following='HerbEaten')
#def claustrophobia_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('claustrophobia', False)
#
#@Trigger(r'^The awful loneliness leaves you\.$',
#         following='HerbEaten')
#def loneliness_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('loneliness', False)
#
#@Trigger(r'^You no longer enjoy pain\.$',
#         following='HerbEaten')
#def masochism_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('masochism', False)
#
#@Trigger(r'^Prudence rules your psyche once again\.$',
#         following='HerbEaten')
#def recklessness_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('recklessness', False)
#
#@Trigger(r'^Your fear of heights subsides\.$',
#         following='HerbEaten')
#def vertigo_cured_by_lobelia(match, event_args):
#    assert event_args['herb'] == 'lobelia'
#    set_status('vertigo', False)
#
#
#@Trigger(r'^The confusion lifts from your mind and it is clear once again\.$',
#         following='HerbEaten')
#def confusion_cured_by_ash(match, event_args):
#    assert event_args['herb'] == 'ash'
#    set_status('confusion', False)
#
#@Trigger(r'^Sanity returns to you and you are no longer demented\.$',
#         following='HerbEaten')
#def dementia_cured_by_ash(match, event_args):
#    assert event_args['herb'] == 'ash'
#    set_status('dementia', False)
#
#@Trigger(r'^Your hallucinations cease to plague you\.$',
#         following='HerbEaten')
#def hallucinations_cured_by_ash(match, event_args):
#    assert event_args['herb'] == 'ash'
#    set_status('hallucinations', False)
#
#@Trigger(r'^You feel a bit more alert and awake\.$',
#         following='HerbEaten')
#def hypersomnia_cured_by_ash(match, event_args):
#    assert event_args['herb'] == 'ash'
#    set_status('hypersomnia', False)
#
#@Trigger(r'^Aaaahhh\. No one is out to get you after all\.$',
#         following='HerbEaten')
#def paranoia_cured_by_ash(match, event_args):
#    assert event_args['herb'] == 'ash'
#    set_status('paranoia', False)
    
# The plant has no effect.
    
@OnEvent('PromptLine', following='HerbEaten')
def herb_eaten_without_effect(event_args):
    herb = event_args['herb']
    if herb == 'cohosh':
        set_status('insomnia', True)
    elif herb == 'kola':
        set_status('kola', True)
    elif herb == 'bellwort':
        for a in ['generosity', 'justice', 'lovers', 'pacifism', 'peace']:
            set_status(a, False)
    elif herb == 'bloodroot':
        for a in ['paralysis', 'slickness']:
            set_status(a, False)
    elif herb == 'ginseng':
        for a in ['addiction', 'darkshade', 'haemophilia', 'lethargy',
                  'nausea', 'scytherus']:
            set_status(a, False)
    elif herb == 'goldenseal':
        for a in ['stupidity', 'epilepsy', 'dissonance', 'dizziness',
                  'impatience', 'shyness']:
            set_status(a, False)
    elif herb == 'kelp':
        for a in ['asthma', 'clumsiness', 'hypochondria', 'sensitivity',
                  'weariness']:
            set_status(a, False)
    elif herb == 'lobelia':
        for a in ['agoraphobia', 'claustrophobia', 'loneliness', 'masochism',
                  'recklessness', 'vertigo']:
            set_status(a, False)
    elif herb == 'ash':
        for a in ['confusion', 'dementia', 'hallucinations', 'hypersomnia',
                  'paranoia']:
            set_status(a, False)
    