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

from curtail_uuid import CurtailUUID
from django.contrib import admin
from django.contrib.auth.hashers import make_password
from django.db import transaction

from account.models import (LensmanIncomeExpensesInfo, LensmanInfo, LensmanLoginLogInfo, TourGuideInfo,
                            UserIncomeExpensesInfo, UserInfo, UserLoginLogInfo)


class LensmanInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('lensman_id', 'encryption', )
    list_display = ('lensman_id', 'unionid', 'username', 'name', 'sex', 'phone', 'location', 'proportion', 'nomark', 'origin', 'balance', 'user_status', 'status', 'created_at', 'updated_at')
    search_fields = ('name', 'phone', 'location')
    list_filter = ('sex', 'user_status', 'status')

    @transaction.atomic
    def save_model(self, request, obj, form, change):
        if not obj.lensman_id:
            try:
                user_id = UserInfo.objects.select_for_update().get(unionid=obj.unionid).user_id
            except UserInfo.DoesNotExist:
                user_id = None
            obj.lensman_id = user_id or CurtailUUID.uuid(UserInfo, 'user_id')

        fields = {
            'user_from': UserInfo.LENSMAN_USER,
            'unionid': obj.unionid,
            'name': obj.name,
            'sex': obj.sex,
            'phone': obj.phone,
            'location': obj.location,
            'islensman': True,
            'user_status': obj.user_status,
        }
        user, created = UserInfo.objects.get_or_create(user_id=obj.lensman_id, defaults=fields)
        if not created:
            fields.pop('user_from', None)
            for key, value in fields.iteritems():
                setattr(user, key, value)
            user.save()

        if obj.password:
            obj.encryption = make_password(obj.password, None, 'pbkdf2_sha256')
            obj.password = None
        obj.save()


class LensmanLoginLogInfoAdmin(admin.ModelAdmin):
    list_display = ('lensman_id', 'login_ip', 'login_result', 'status', 'created_at', 'updated_at')


class LensmanIncomeExpensesInfoAdmin(admin.ModelAdmin):
    list_display = ('lensman_id', 'photo_id', 'type', 'amount', 'balance', 'remark', 'status', 'created_at', 'updated_at')
    list_filter = ('type', 'status')


class TourGuideInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('tourguide_id', )
    list_display = ('tourguide_id', 'unionid', 'name', 'sex', 'phone', 'location', 'no', 'user_status', 'status', 'created_at', 'updated_at')
    search_fields = ('name', 'phone', 'location', 'no')
    list_filter = ('sex', 'user_status', 'status')

    @transaction.atomic
    def save_model(self, request, obj, form, change):
        if not obj.tourguide_id:
            try:
                user_id = UserInfo.objects.select_for_update().get(unionid=obj.unionid).user_id
            except UserInfo.DoesNotExist:
                user_id = None
            obj.tourguide_id = user_id or CurtailUUID.uuid(UserInfo, 'user_id')
            obj.save()

        fields = {
            'user_from': UserInfo.TOURGUIDE_USER,
            'unionid': obj.unionid,
            'name': obj.name,
            'sex': obj.sex,
            'phone': obj.phone,
            'location': obj.location,
            'istourguide': True,
            'user_status': obj.user_status,
        }
        user, created = UserInfo.objects.get_or_create(user_id=obj.tourguide_id, defaults=fields)
        if not created:
            fields.pop('user_from', None)
            for key, value in fields.iteritems():
                setattr(user, key, value)
            user.save()


class UserInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('user_id', )
    list_display = ('user_id', 'user_from', 'username', 'openid', 'unionid', 'name', 'sex', 'nickname', 'phone', 'location', 'balance', 'user_status', 'status', 'created_at', 'updated_at')
    search_fields = ('name', 'phone', 'location')
    list_filter = ('user_from', 'sex', 'user_status', 'status')


class UserLoginLogInfoAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'login_ip', 'login_result', 'status', 'created_at', 'updated_at')


class UserIncomeExpensesInfoAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'photo_id', 'type', 'amount', 'balance', 'remark', 'status', 'created_at', 'updated_at')
    list_filter = ('type', 'status')


admin.site.register(LensmanInfo, LensmanInfoAdmin)
admin.site.register(LensmanLoginLogInfo, LensmanLoginLogInfoAdmin)
admin.site.register(LensmanIncomeExpensesInfo, LensmanIncomeExpensesInfoAdmin)
admin.site.register(TourGuideInfo, TourGuideInfoAdmin)
admin.site.register(UserInfo, UserInfoAdmin)
admin.site.register(UserLoginLogInfo, UserLoginLogInfoAdmin)
admin.site.register(UserIncomeExpensesInfo, UserIncomeExpensesInfoAdmin)
