# Path: ledgerbil/colorable.py
# class Colorable:
# 
#     START_CODE = "\033"
#     END_CODE = f"{START_CODE}[0m"
# 
#     BRIGHT_OFFSET = 60
# 
#     COLORS = {
#         "black": 30,
#         "gray": 30,
#         "grey": 30,
#         "red": 31,
#         "green": 32,
#         "yellow": 33,
#         "blue": 34,
#         "magenta": 35,
#         "purple": 35,
#         "cyan": 36,
#         "white": 37,
#     }
# 
#     ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
# 
#     def __init__(self, color, value, fmt="", bright=False):
# 
#         if color not in self.COLORS:
#             raise UnsupportedColorError(
#                 f"I don't know what to do with this color: {color}"
#             )
# 
#         self.my_color = color
#         self.value = value
#         self.bright = bright
#         self.format_string = fmt
# 
#     def __repr__(self):
#         return (
#             f"Colorable('{self.my_color}', '{self.value}', "
#             f"fmt='{self.format_string}', bright={self.bright})"
#         )
# 
#     def __str__(self):
#         start = self.ansi_sequence(self.COLORS[self.my_color], bright=self.bright)
#         ansi_str = f"{start}{self.value:{self.format_string}}{self.END_CODE}"
#         return ansi_str
# 
#     def __len__(self):
#         return len(self.value)
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def ansi_sequence(self, code, bright=False):
#         offset = 60 if bright else 0
#         color = code + offset
#         return f"{self.START_CODE}[0;{color}m"
# 
#     def plain(self):
#         return self.value
# 
#     @staticmethod
#     def get_plain_string(ansi_string):
#         return Colorable.ansi_escape.sub("", ansi_string)
# 
# Path: ledgerbil/ledgerbilexceptions.py
# class LdgPortfolioError(LdgException):
#     pass
# 
# Path: ledgerbil/settings_getter.py
# def get_setting(setting, default=None):
#     if settings and hasattr(settings, setting):
#         return getattr(settings, setting)
# 
#     return defaults.get(setting, default)


import argparse
import itertools
import json
import re
from collections import defaultdict, namedtuple
from operator import attrgetter
from textwrap import dedent
from . import util
from .colorable import Colorable
from .ledgerbilexceptions import LdgPortfolioError
from .settings_getter import get_setting


def get_comparison_report_line(comparison_item, percent, labels):
    col1_width = COL_LABEL if labels else COL_ACCOUNT
    col1 = Colorable("blue", comparison_item.col1, col1_width)

    if comparison_item.value == 0:
        value = " " * COL_VALUE
        percent_f = " " * COL_PERCENT
    else:
        value = util.get_plain_amount(comparison_item.value, COL_VALUE, 0)
        percent_f = f"{percent:>{COL_PERCENT}.0f}"
    gain_value = util.get_colored_amount(
        comparison_item.gain_value, colwidth=COL_GAIN_VALUE, decimals=0
    )
    num_years = f"{comparison_item.num_years:>{COL_NUM_YEARS}}"

    gain_all = get_formatted_gain(annualized_total=comparison_item.all)
    gain_1 = get_formatted_gain(annualized_total=comparison_item.y1)
    gain_3 = get_formatted_gain(annualized_total=comparison_item.y3)
    gain_5 = get_formatted_gain(annualized_total=comparison_item.y5)
    gain_10 = get_formatted_gain(annualized_total=comparison_item.y10)

    return (
        f"{col1}  {value}  {percent_f}  {gain_value}  {num_years}  "
        f"{gain_all}  {gain_1}  {gain_3}  {gain_5}  {gain_10}\n"
    )


def get_portfolio_data():

# Path: ledgerbil/colorable.py
# class Colorable:
# 
#     START_CODE = "\033"
#     END_CODE = f"{START_CODE}[0m"
# 
#     BRIGHT_OFFSET = 60
# 
#     COLORS = {
#         "black": 30,
#         "gray": 30,
#         "grey": 30,
#         "red": 31,
#         "green": 32,
#         "yellow": 33,
#         "blue": 34,
#         "magenta": 35,
#         "purple": 35,
#         "cyan": 36,
#         "white": 37,
#     }
# 
#     ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
# 
#     def __init__(self, color, value, fmt="", bright=False):
# 
#         if color not in self.COLORS:
#             raise UnsupportedColorError(
#                 f"I don't know what to do with this color: {color}"
#             )
# 
#         self.my_color = color
#         self.value = value
#         self.bright = bright
#         self.format_string = fmt
# 
#     def __repr__(self):
#         return (
#             f"Colorable('{self.my_color}', '{self.value}', "
#             f"fmt='{self.format_string}', bright={self.bright})"
#         )
# 
#     def __str__(self):
#         start = self.ansi_sequence(self.COLORS[self.my_color], bright=self.bright)
#         ansi_str = f"{start}{self.value:{self.format_string}}{self.END_CODE}"
#         return ansi_str
# 
#     def __len__(self):
#         return len(self.value)
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def ansi_sequence(self, code, bright=False):
#         offset = 60 if bright else 0
#         color = code + offset
#         return f"{self.START_CODE}[0;{color}m"
# 
#     def plain(self):
#         return self.value
# 
#     @staticmethod
#     def get_plain_string(ansi_string):
#         return Colorable.ansi_escape.sub("", ansi_string)
# 
# Path: ledgerbil/ledgerbilexceptions.py
# class LdgPortfolioError(LdgException):
#     pass
# 
# Path: ledgerbil/settings_getter.py
# def get_setting(setting, default=None):
#     if settings and hasattr(settings, setting):
#         return getattr(settings, setting)
# 
#     return defaults.get(setting, default)


# Path: ledgerbil/portfolio.py
import argparse
import itertools
import json
import re
from collections import defaultdict, namedtuple
from operator import attrgetter
from textwrap import dedent
from . import util
from .colorable import Colorable
from .ledgerbilexceptions import LdgPortfolioError
from .settings_getter import get_setting



def get_comparison_report_line(comparison_item, percent, labels):
    col1_width = COL_LABEL if labels else COL_ACCOUNT
    col1 = Colorable("blue", comparison_item.col1, col1_width)

    if comparison_item.value == 0:
        value = " " * COL_VALUE
        percent_f = " " * COL_PERCENT
    else:
        value = util.get_plain_amount(comparison_item.value, COL_VALUE, 0)
        percent_f = f"{percent:>{COL_PERCENT}.0f}"
    gain_value = util.get_colored_amount(
        comparison_item.gain_value, colwidth=COL_GAIN_VALUE, decimals=0
    )
    num_years = f"{comparison_item.num_years:>{COL_NUM_YEARS}}"

    gain_all = get_formatted_gain(annualized_total=comparison_item.all)
    gain_1 = get_formatted_gain(annualized_total=comparison_item.y1)
    gain_3 = get_formatted_gain(annualized_total=comparison_item.y3)
    gain_5 = get_formatted_gain(annualized_total=comparison_item.y5)
    gain_10 = get_formatted_gain(annualized_total=comparison_item.y10)

    return (
        f"{col1}  {value}  {percent_f}  {gain_value}  {num_years}  "
        f"{gain_all}  {gain_1}  {gain_3}  {gain_5}  {gain_10}\n"
    )


def get_portfolio_data():

    with open(get_setting("PORTFOLIO_FILE"), "r", encoding="utf-8") as portfile: