# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/forms.py
# class MinecraftAccountForm(forms.Form):
#     username = forms.CharField(label=_('Username'))
#     password = forms.CharField(
#         label=_('Password'),
#         help_text=_('Your password will not be stored in our database'),
#         widget=forms.PasswordInput)
# 
#     class Meta:
#         fields = ()
# 
#     def __init__(self, *args, **kwargs):
#         super(MinecraftAccountForm, self).__init__(*args, **kwargs)
#         self.profiles = []
# 
#     def clean(self):
#         cleaned_data = super(MinecraftAccountForm, self).clean()
# 
#         username = self.cleaned_data.get('username')
#         password = self.cleaned_data.get('password')
# 
#         try:
#             response = authenticate(username, password)
#         except:
#             raise forms.ValidationError(_(
#                 'You entered invalid credentials. '
#                 'Username or password are incorrect.'))
#         else:
#             selected_profile = response.get('selectedProfile')
# 
#             # Add selected profile
#             profile_id = selected_profile.get('id')
# 
#             try:
#                 MinecraftAccount.objects.get(profile_id=profile_id)
#             except MinecraftAccount.DoesNotExist:
#                 self.profiles.append(selected_profile)
#             else:
#                 raise forms.ValidationError(_(
#                     'This Minecraft Profile is already being used '
#                     'by another account.'))
# 
#             # Append other profiles
#             for other_profile in response.get('availableProfiles'):
#                 if other_profile.get('id') != profile_id:
#                     self.profiles.append(other_profile)
# 
#         return cleaned_data
# 
#     def save(self, user):
#         is_first_profile = not user.minecraft_accounts.exists()
# 
#         # Save profiles
#         for order, profile in enumerate(self.profiles):
#             primary = is_first_profile and order == 0
#             MinecraftAccount.objects.create(
#                 user=user,
#                 profile=profile.get('name'),
#                 profile_id=profile.get('id'),
#                 primary=primary,
#             )


from django.views.generic import ListView, FormView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect, resolve_url
from braces.views import LoginRequiredMixin
from .models import MinecraftAccount
from .forms import MinecraftAccountForm




class MinecraftViewMixin(LoginRequiredMixin):
    pass


class MinecraftList(MinecraftViewMixin, ListView):
    template_name = 'mineid/mcaccounts/list.html'

    def get_queryset(self):
        return MinecraftAccount.objects.filter(user=self.request.user)


class MinecraftCreate(MinecraftViewMixin, FormView):
    form_class = MinecraftAccountForm
    template_name = 'mineid/mcaccounts/create.html'
    success_url = reverse_lazy('minecraft:list')

    def form_valid(self, form):
        form.save(self.request.user)
        return redirect(self.get_success_url())


class MinecraftDelete(MinecraftViewMixin, DeleteView):

# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/forms.py
# class MinecraftAccountForm(forms.Form):
#     username = forms.CharField(label=_('Username'))
#     password = forms.CharField(
#         label=_('Password'),
#         help_text=_('Your password will not be stored in our database'),
#         widget=forms.PasswordInput)
# 
#     class Meta:
#         fields = ()
# 
#     def __init__(self, *args, **kwargs):
#         super(MinecraftAccountForm, self).__init__(*args, **kwargs)
#         self.profiles = []
# 
#     def clean(self):
#         cleaned_data = super(MinecraftAccountForm, self).clean()
# 
#         username = self.cleaned_data.get('username')
#         password = self.cleaned_data.get('password')
# 
#         try:
#             response = authenticate(username, password)
#         except:
#             raise forms.ValidationError(_(
#                 'You entered invalid credentials. '
#                 'Username or password are incorrect.'))
#         else:
#             selected_profile = response.get('selectedProfile')
# 
#             # Add selected profile
#             profile_id = selected_profile.get('id')
# 
#             try:
#                 MinecraftAccount.objects.get(profile_id=profile_id)
#             except MinecraftAccount.DoesNotExist:
#                 self.profiles.append(selected_profile)
#             else:
#                 raise forms.ValidationError(_(
#                     'This Minecraft Profile is already being used '
#                     'by another account.'))
# 
#             # Append other profiles
#             for other_profile in response.get('availableProfiles'):
#                 if other_profile.get('id') != profile_id:
#                     self.profiles.append(other_profile)
# 
#         return cleaned_data
# 
#     def save(self, user):
#         is_first_profile = not user.minecraft_accounts.exists()
# 
#         # Save profiles
#         for order, profile in enumerate(self.profiles):
#             primary = is_first_profile and order == 0
#             MinecraftAccount.objects.create(
#                 user=user,
#                 profile=profile.get('name'),
#                 profile_id=profile.get('id'),
#                 primary=primary,
#             )


# Path: mineid/mcaccounts/views.py
from django.views.generic import ListView, FormView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect, resolve_url
from braces.views import LoginRequiredMixin
from .models import MinecraftAccount
from .forms import MinecraftAccountForm





class MinecraftViewMixin(LoginRequiredMixin):
    pass


class MinecraftList(MinecraftViewMixin, ListView):
    template_name = 'mineid/mcaccounts/list.html'

    def get_queryset(self):
        return MinecraftAccount.objects.filter(user=self.request.user)


class MinecraftCreate(MinecraftViewMixin, FormView):
    form_class = MinecraftAccountForm
    template_name = 'mineid/mcaccounts/create.html'
    success_url = reverse_lazy('minecraft:list')

    def form_valid(self, form):
        form.save(self.request.user)
        return redirect(self.get_success_url())


class MinecraftDelete(MinecraftViewMixin, DeleteView):

    model = MinecraftAccount