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

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

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


class AdministratorInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('admin_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 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_full_name', 'model_descr', 'image', 'url', 'factory_yuan', 'integral', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'status')
    readonly_fields = ('brand_name', 'factory_fee')
    search_fields = ('brand_id', 'brand_name', 'model_id', 'model_name', 'model_full_name', 'model_descr')

    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.factory_fee = monetary.Yuan2Fen(obj.factory_yuan)
        obj.factory_yuan = monetary.Fen2Yuan(obj.factory_fee)

        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_province_name', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'distributor_province_name', 'status')
    readonly_fields = ('brand_name', 'distributor_province_code')
    search_fields = ('brand_id', 'brand_name', 'distributor_name', 'distributor_descr', 'distributor_province_code', 'distributor_province_name')

    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.distributor_province_code = ProvinceShortModelMixin.PROVINCE_NAME_CODE_DICT.get(obj.distributor_province_name, '')

        obj.save()


class BrandModelDistributorPriceInfoAdmin(DeleteModelAdmin, admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'model_id', 'model_name', 'distributor_id', 'distributor_name', 'factory_yuan', 'integral', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'model_name', 'distributor_name', 'status')
    readonly_fields = ('brand_name', 'model_name', 'distributor_name', 'factory_fee', )
    search_fields = ('brand_id', 'brand_name', 'model_id', 'model_name', 'distributor_id', 'distributor_name')

    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.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.distributor_id = strip(obj.distributor_id)
        try:
            obj.distributor_name = DistributorInfo.objects.get(distributor_id=obj.distributor_id).distributor_name
        except BrandInfo.DoesNotExist:
            obj.distributor_name = ''

        obj.factory_fee = monetary.Yuan2Fen(obj.factory_yuan)
        obj.factory_yuan = monetary.Fen2Yuan(obj.factory_fee)

        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', 'test_user', 'status', 'created_at', 'updated_at')
    list_filter = ('test_user', 'verifyResult', 'status')


admin.site.register(AdministratorInfo, AdministratorInfoAdmin)
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(BrandModelDistributorPriceInfo, BrandModelDistributorPriceInfoAdmin)
admin.site.register(LatestAppInfo, LatestAppInfoAdmin)
admin.site.register(ConsumeInfoSubmitLogInfo, ConsumeInfoSubmitLogInfoAdmin)
