# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import json
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot.inventory import pokemons, Pokemon, Attack

import re


DEFAULT_IGNORE_FAVORITES = False
DEFAULT_GOOD_ATTACK_THRESHOLD = 0.7
DEFAULT_TEMPLATE = '{name}'

MAXIMUM_NICKNAME_LENGTH = 12


class NicknamePokemon(BaseTask):
    SUPPORTED_TASK_API_VERSION = 1

    """
    Nickname user pokemons according to the specified template


    PARAMETERS:

    dont_nickname_favorite (default: False)
        Prevents renaming of favorited pokemons

    good_attack_threshold (default: 0.7)
        Threshold for perfection of the attack in it's type (0.0-1.0)
         after which attack will be treated as good.
        Used for {fast_attack_char}, {charged_attack_char}, {attack_code}
         templates

    nickname_template (default: '{name}')
        Template for nickname generation.
        Empty template or any resulting in the simple pokemon name
         (e.g. '', '{name}', ...) will revert all pokemon to their original
         names (as if they had no nickname).

        Niantic imposes a 12-character limit on all pokemon nicknames, so
         any new nickname will be truncated to 12 characters if over that limit.
        Thus, it is up to the user to exercise judgment on what template will
         best suit their need with this constraint in mind.

        You can use full force of the Python [Format String syntax](https://docs.python.org/2.7/library/string.html#formatstrings)
        For example, using `{name:.8s}` causes the Pokemon name to never take up
         more than 8 characters in the nickname. This would help guarantee that
         a template like `{name:.8s}_{iv_pct}` never goes over the 12-character
         limit.


    **NOTE:** If you experience frequent `Pokemon not found` error messages,
     this is because the inventory cache has not been updated after a pokemon
     was released. This can be remedied by placing the `NicknamePokemon` task
     above the `TransferPokemon` task in your `config.json` file.


    EXAMPLE CONFIG:
    {
      "type": "NicknamePokemon",
      "config": {
        "enabled": true,
        "dont_nickname_favorite": false,
        "good_attack_threshold": 0.7,
        "nickname_template": "{iv_pct}_{iv_ads}"
      }
    }


    SUPPORTED PATTERN KEYS:

    {name}  Pokemon name      (e.g. Articuno)
    {id}    Pokemon ID/Number (1-151)
    {cp}    Combat Points     (10-4145)

    # Individial Values
    {iv_attack}  Individial Attack (0-15) of the current specific pokemon
    {iv_defense} Individial Defense (0-15) of the current specific pokemon
    {iv_stamina} Individial Stamina (0-15) of the current specific pokemon
    {iv_ads}     Joined IV values (e.g. 4/12/9)
    {iv_sum}     Sum of the Individial Values (0-45)
    {iv_pct}     IV perfection (in 000-100 format - 3 chars)
    {iv_pct2}    IV perfection (in 00-99 format - 2 chars)
                    So 99 is best (it's a 100% perfection)