import balancesheet.mongoData.equities_data_service as dsvce
from userInteraction.financeCliInteraction import FinanceCliInteraction
import ledgerkeeper.mongoData.account_data_service as dsvca
from balancesheet.enums import EquityClass, AssetType, LiabiltyType, EquityTimeHorizon, EquityStatus, EquityContingency
import plotter as plot

class EquityManager():
    def __init__(self, user_notification_system: FinanceCliInteraction):
        self.uns = user_notification_system


    def add_equity(self):
        name = self.uns.request_string("Name: ")
        description = self.uns.request_string("Description: ")
        accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())
        equityClass = self.uns.request_enum(EquityClass)
        if equityClass == EquityClass.ASSET:
            equityType = self.uns.request_enum(AssetType)
        elif equityClass == EquityClass.LIABILITY:
            equityType = self.uns.request_enum(LiabiltyType)
        else:
            raise Exception(f"Unknown equity class: {equityClass.name}")

        interestRate = self.uns.request_float("Interest Rate: ")
        equityTimeHorizon = self.uns.request_enum(EquityTimeHorizon)
        equityStatus = self.uns.request_enum(EquityStatus)
        equityContingency = self.uns.request_enum(EquityContingency)

        equity = dsvce.enter_if_not_exists(name=name,
                                           description=description,
                                           accountId=str(dsvca.account_by_name(accountName).id),
                                           equityClass=equityClass,
                                           equityType=equityType,
                                           equityTimeHorizon=equityTimeHorizon,
                                           equityStatus=equityStatus,
                                           equityContingency=equityContingency,
                                           interestRate=interestRate)
    
        if equity is not None:
            self.uns.notify_user("Equity entered successfully!")

    def delete_equity(self):
        accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())
        equityName = self.uns.request_from_dict(dsvce.equities_as_dict())

        dsvce.delete_equity(dsvca.account_by_name(accountName).id, equityName)

    def record_value(self):
        accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())
        equityName = self.uns.request_from_dict(dsvce.equities_as_dict())
        year = self.uns.request_int("Year: ")
        month = self.uns.request_int("Month: ")
        value = self.uns.request_float("Value: ")


        account = dsvca.account_by_name(accountName)
        equity = dsvce.equity_by_account_and_name(str(account.id), equityName)
        if equity is None:
            raise Exception(f"Equity: {accountName} [{account.id}], {equityName} not found.")
        value = dsvce.record_value_on_equity(equity, year, month, value)
        
        if value is not None:
            self.uns.notify_user("Value Recorded successfully!")

    def print_value_snapshots(self, accountName=None):
        if accountName is None:
            accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())

        account = dsvca.account_by_name(accountName)

        equities = dsvce.equities_by_account(account.id)

        if equities is None or len(equities) == 0:
            self.uns.notify_user(f"No Equities in account [{accountName}]")
            return

        self.uns.pretty_print_items(sorted(equities, key=lambda x: x.equityType),
                                    title="Equities Snapshots")

    def print_equities(self):
        self.uns.pretty_print_items(dsvce.query_equities("").to_json(), title="Equities")

    def print_balance_sheet(self):
        accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())
        relevant_mos = self.uns.request_int("Number of past months: ")
        account = dsvca.account_by_name(accountName)
        
        data = dsvce.balance_sheet_over_time(relevant_months=relevant_mos, accountIds=[str(account.id)])

        self.uns.notify_user(f"\n---------Balance Sheet---------")
        self.uns.pretty_print_items(data)

    def plot_balance_over_time(self):
        relevant_mos = self.uns.request_int("Number of past months: ")
        accountName = self.uns.request_from_dict(dsvca.accounts_as_dict())
        account = dsvca.account_by_name(accountName)

        ax = plot.plot_assets_liabilities_worth_over_time(relevant_mos, accountIds=[str(account.id)])
        if ax is None:
            self.uns.notify_user("No Data to show...")







[((56, 18, 56, 52), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(56, 40, 56, 51): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((60, 16, 60, 72), 'balancesheet.mongoData.equities_data_service.record_value_on_equity', 'dsvce.record_value_on_equity', ({(60, 45, 60, 51): 'equity', (60, 53, 60, 57): 'year', (60, 59, 60, 64): 'month', (60, 66, 60, 71): 'value'}, {}), '(equity, year, month, value)', True, 'import balancesheet.mongoData.equities_data_service as dsvce\n'), ((69, 18, 69, 52), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(69, 40, 69, 51): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((71, 19, 71, 56), 'balancesheet.mongoData.equities_data_service.equities_by_account', 'dsvce.equities_by_account', ({(71, 45, 71, 55): 'account.id'}, {}), '(account.id)', True, 'import balancesheet.mongoData.equities_data_service as dsvce\n'), ((86, 18, 86, 52), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(86, 40, 86, 51): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((96, 18, 96, 52), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(96, 40, 96, 51): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((15, 49, 15, 73), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((43, 49, 43, 73), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((44, 48, 44, 72), 'balancesheet.mongoData.equities_data_service.equities_as_dict', 'dsvce.equities_as_dict', ({}, {}), '()', True, 'import balancesheet.mongoData.equities_data_service as dsvce\n'), ((49, 49, 49, 73), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((50, 48, 50, 72), 'balancesheet.mongoData.equities_data_service.equities_as_dict', 'dsvce.equities_as_dict', ({}, {}), '()', True, 'import balancesheet.mongoData.equities_data_service as dsvce\n'), ((84, 49, 84, 73), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((95, 49, 95, 73), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((46, 28, 46, 62), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(46, 50, 46, 61): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((67, 53, 67, 77), 'ledgerkeeper.mongoData.account_data_service.accounts_as_dict', 'dsvca.accounts_as_dict', ({}, {}), '()', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n'), ((81, 36, 81, 60), 'balancesheet.mongoData.equities_data_service.query_equities', 'dsvce.query_equities', ({(81, 57, 81, 59): '""""""'}, {}), "('')", True, 'import balancesheet.mongoData.equities_data_service as dsvce\n'), ((31, 57, 31, 91), 'ledgerkeeper.mongoData.account_data_service.account_by_name', 'dsvca.account_by_name', ({(31, 79, 31, 90): 'accountName'}, {}), '(accountName)', True, 'import ledgerkeeper.mongoData.account_data_service as dsvca\n')]