# MongoEngine imports
import mongoengine as models

# Joker Model Mixin imports
from seocortex.joker.yahoo.mixin_models import YahooAccountMixin
from seocortex.joker.twitter.mixin_models import TwitterAccountMixin
from seocortex.joker.pinterest.mixin_models import PinterestAccountMixin


# No _cls or _types fields (generated by MongoEngine)
class CleanDocumentMixin(object):
    meta = { 'allow_inheritance' : False }


# Embedded docs
class YahooAccountEmbedded(models.EmbeddedDocument, YahooAccountMixin, CleanDocumentMixin):
    pass

class TwitterAccountEmbedded(models.EmbeddedDocument, TwitterAccountMixin, CleanDocumentMixin):
    pass

class PinterestAccountEmbedded(models.EmbeddedDocument, PinterestAccountMixin, CleanDocumentMixin):
    pass



# An embedded doc to encapsulate the others
class AccountEmbedded(models.EmbeddedDocument, CleanDocumentMixin):
    yahoo = models.EmbeddedDocumentField(YahooAccountEmbedded)
    twitter = models.EmbeddedDocumentField(TwitterAccountEmbedded)
    pinterest = models.EmbeddedDocumentField(PinterestAccountEmbedded)


class JokerProfile(models.Document, CleanDocumentMixin):
    accounts = models.EmbeddedDocumentField(AccountEmbedded, required = True)

    def add_account(self, account):
        if self.accounts is None:
            self.accounts = AccountEmbedded()
        if isinstance(account, YahooAccountMixin):
            self.accounts.yahoo = account
        elif isinstance(account, TwitterAccountMixin):
            self.accounts.twitter = account
        elif isinstance(account, PinterestAccountMixin):
            self.accounts.pinterest = account
        else:
            return False
        return True

    def remove_account(self, account_name):
        if name in ('yahoo', 'twitter', 'pinterest'):
            delattr(self.accounts, account_name)
            return True
        return False