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

from django.conf import settings
from django.contrib import admin
from django.contrib.auth.hashers import check_password, make_password
from django_admin import DeleteModelAdmin, ReadOnlyModelAdmin
from pysnippets.strsnippets import strip

from mch.models import (BrandInfo, ConsumeInfoSubmitLogInfo, DistributorInfo, LatestAppInfo, ModelImageInfo, ModelInfo,
                        OperatorInfo)


class OperatorInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('operator_id', 'phone', 'password', 'encryption', 'name', 'brand_id', 'brand_name', 'user_status', 'status', 'created_at', 'updated_at')
    list_filter = ('user_status', 'status', 'brand_name')
    readonly_fields = ('encryption', 'brand_name')

    def save_model(self, request, obj, form, change):
        obj.phone = strip(obj.phone)
        obj.password = strip(obj.password)
        if obj.password:
            obj.encryption = make_password(obj.password, settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)
        obj.password = ''

        obj.brand_id = strip(obj.brand_id)
        try:
            obj.brand_name = BrandInfo.objects.get(brand_id=obj.brand_id).brand_name
        except BrandInfo.DoesNotExist:
            obj.brand_name = ''

        obj.save()


class BrandInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'brand_descr', 'brand_logo', 'position', 'status', 'created_at', 'updated_at')


class ModelInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'model_id', 'model_name', 'model_descr', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'status')

    def save_model(self, request, obj, form, change):
        obj.brand_id = strip(obj.brand_id)
        try:
            obj.brand_name = BrandInfo.objects.get(brand_id=obj.brand_id).brand_name
        except BrandInfo.DoesNotExist:
            obj.brand_name = ''

        obj.save()


class ModelImageInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('model_id', 'model_name', 'image', 'url', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('model_name', 'status')

    def save_model(self, request, obj, form, change):
        obj.model_id = strip(obj.model_id)
        try:
            obj.model_name = ModelInfo.objects.get(model_id=obj.model_id).model_name
        except BrandInfo.DoesNotExist:
            obj.model_name = ''

        obj.save()


class DistributorInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'distributor_id', 'distributor_name', 'distributor_descr', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'status')

    def save_model(self, request, obj, form, change):
        obj.brand_id = strip(obj.brand_id)
        try:
            obj.brand_name = BrandInfo.objects.get(brand_id=obj.brand_id).brand_name
        except BrandInfo.DoesNotExist:
            obj.brand_name = ''

        obj.save()


class LatestAppInfoAdmin(admin.ModelAdmin):
    list_display = ('latest_adr_version_code', 'latest_adr_version_name', 'latest_adr_app', 'latest_adr_url', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )
    readonly_fields = ('status', )

    def save_model(self, request, obj, form, change):
        if not obj.pk and LatestAppInfo.objects.filter(status=True).exists():
            return
        obj.latest_adr_version_name = strip(obj.latest_adr_version_name)
        obj.latest_adr_app = strip(obj.latest_adr_app)
        obj.latest_adr_url = strip(obj.latest_adr_url)
        obj.save()


class ConsumeInfoSubmitLogInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin):
    list_display = ('user_id', 'phone', 'lat', 'lon', 'serialNo', 'verifyResult', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )


# admin.site.register(OperatorInfo, OperatorInfoAdmin)
# admin.site.register(BrandInfo, BrandInfoAdmin)
# admin.site.register(ModelInfo, ModelInfoAdmin)
# admin.site.register(ModelImageInfo, ModelImageInfoAdmin)
# admin.site.register(DistributorInfo, DistributorInfoAdmin)
# admin.site.register(LatestAppInfo, LatestAppInfoAdmin)
# admin.site.register(ConsumeInfoSubmitLogInfo, ConsumeInfoSubmitLogInfoAdmin)
