# -*- coding: utf-8 -*-

from libturpial.api.models.column import Column
from libturpial.exceptions import ColumnAlreadyRegistered
from libturpial.common import get_account_id_from, get_column_slug_from

class ColumnManager:
    """
    This class has methods to manage columns. You can register new columns,
    load and unregister existing columns.

    This manager can be iterated and each element will have a list of columns
    per account. For example:

    >>> for item in column_manager:
            print item
    ('foo-twitter', [<libturpial.api.models.column.Column instance at 0x10a9ce368>, 
        <libturpial.api.models.column.Column instance at 0x10a9ce8c0>])

    """
    def __init__(self, config):
        self.config = config
        self.__load_registered()

    def __len__(self):
        return len(self.__registered_columns)

    def __iter__(self):
        return self.__registered_columns.iteritems()

    def __load_registered(self):
        self.__registered_columns = {}

        for column_id in self.config.get_stored_columns():
            account_id = get_account_id_from(column_id)
            column_slug = get_column_slug_from(column_id)
            if not self.__registered_columns.has_key(account_id):
                self.__registered_columns[account_id] = []
            self.__registered_columns[account_id].append(Column(account_id, column_slug))

    def __count(self):
        count = 0
        for account_id, columns in self.__registered_columns.iteritems():
            for col in columns:
                count += 1
        return count

    def register(self, column_id):
        """
        Register a new column identified by *column_id*. If the column is 
        already registered a
        :class:`libturpial.exceptions.ColumnAlreadyRegistered` exception will
        raise. Return the id of the column registered on success
        """
        for account_id, columns in self.__registered_columns.iteritems():
            for col in columns:
                if col.id_ == column_id:
                    raise ColumnAlreadyRegistered

        count = self.__count() + 1
        key = "column%s" % count
        self.config.write('Columns', key, column_id)

        self.__load_registered()
        return column_id

    def unregister(self, column_id):
        """
        Remove the column identified by *column_id* from memory. Return the id 
        of the unregistered column.
        """
        index = 0
        to_store = {}
        for account_id, columns in self.__registered_columns.iteritems():
            for col in columns:
                if col.id_ != column_id:
                    index += 1
                    key = "column%s" % index
                    to_store[key] = col.id_
        self.config.write_section('Columns', to_store)

        self.__load_registered()
        return column_id

    def columns(self):
        """
        Return a dict where each key represents an account_id and it value is 
        a list of :class:`libturpial.api.models.column.Column` objects
        with all the columns registered.

        For example:

        >>> column_manager.columns()
        {'foo-twitter': [<libturpial.api.models.column.Column instance at 0x10a9cbb48>, 
            <libturpial.api.models.column.Column instance at 0x10a9cb6c8>]}
        """
        return self.__registered_columns

    def is_registered(self, column_id):
        """
        Return `True` if column identified by *column_id* is registered. 
        `False` otherwise.
        """
        for account_id, columns in self.__registered_columns.iteritems():
            for col in columns:
                if col.id_ == column_id:
                    return True
        return False

