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

from djadmin import AdvancedExportExcelModelAdmin, ReadOnlyModelAdmin
from django.conf import settings
from django.contrib import admin
from django.template.loader import render_to_string
from pysnippets.strsnippets import strip

from operation.models import (APPSettingsInfo, BoxProgramVersionInfo, FeedbackInfo, GuestEntranceControlInfo,
                              LatestAppInfo, PatchInfo, SplashInfo)
from utils.disk_utils import write_to_disk
from utils.redis.rapp import set_latest_app
from utils.redis.rguest import delete_guest_entrance_control, set_guest_entrance_control
from utils.redis.roperation.rbox_program_version import set_box_program_version
from utils.redis.rpatch import del_app_patch_info, set_app_patch_info
from utils.redis.rsettings import del_app_settings_info, set_app_settings_info


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

    def save_model(self, request, obj, form, change):
        if not obj.pk and LatestAppInfo.objects.filter(src=obj.src, 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.latest_ios_version_name = strip(obj.latest_ios_version_name)
        obj.latest_ios_url = strip(obj.latest_ios_url)
        obj.save()

        if obj.src == LatestAppInfo.PAIAI_USER:
            # 更新安卓下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_adr_version_name,
            }), settings.PAI2_USER_DOWNLOAD_ADR_PAGE_PATH)

            # 更新 iOS 下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_ios_version_name,
            }), settings.PAI2_USER_DOWNLOAD_IOS_PAGE_PATH)

        elif obj.src == LatestAppInfo.PAIAI_LENSMAN:
            # 更新安卓下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_adr_version_name,
            }), settings.PAI2_LENSMAN_DOWNLOAD_ADR_PAGE_PATH)

            # 更新 iOS 下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_ios_version_name,
            }), settings.PAI2_LENSMAN_DOWNLOAD_IOS_PAGE_PATH)

        elif obj.src == LatestAppInfo.PAIAI_TOURGUIDE:
            # 更新安卓下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_adr_version_name,
            }), settings.PAI2_TOURGUIDE_DOWNLOAD_ADR_PAGE_PATH)

            # 更新 iOS 下载页面
            write_to_disk(render_to_string('page/download.tmpl.html', {
                'src': obj.src,
                'version': obj.latest_ios_version_name,
            }), settings.PAI2_TOURGUIDE_DOWNLOAD_IOS_PAGE_PATH)

        # 设置最新 APP 信息
        set_latest_app(obj.src)

    def delete_model(self, request, obj):
        obj.delete()

        # 设置最新 APP 信息
        set_latest_app(obj.src)


class PatchInfoAdmin(admin.ModelAdmin):
    list_display = ('platform', 'version', 'patch', 'src', 'status', 'created_at', 'updated_at')
    list_filter = ('platform', 'src', 'status')

    def save_model(self, request, obj, form, change):
        obj.version = strip(obj.version)
        obj.save()

        # 设置 APP 补丁信息
        set_app_patch_info(obj)

    def delete_model(self, request, obj):
        obj.delete()

        # 删除 APP 补丁信息
        del_app_patch_info(obj)


class APPSettingsInfoAdmin(admin.ModelAdmin):
    list_display = ('platform', 'channel', 'version', 'online', 'status', 'created_at', 'updated_at')
    list_filter = ('platform', 'online', 'status')

    def save_model(self, request, obj, form, change):
        obj.channel = strip(obj.channel)
        obj.version = strip(obj.version)
        obj.save()

        # 设置 APP 设置信息
        set_app_settings_info(obj)

    def delete_model(self, request, obj):
        obj.delete()

        # 删除 APP 设置信息
        del_app_settings_info(obj)


class SplashInfoAdmin(admin.ModelAdmin):
    list_display = ('splash_image', 'spalash_image_airtime', 'spalash_image_deadline', 'src', 'status', 'created_at', 'updated_at')
    list_filter = ('src', 'status')


class FeedbackInfoAdmin(AdvancedExportExcelModelAdmin, ReadOnlyModelAdmin, admin.ModelAdmin):
    list_display = ('user_id', 'feedback', 'src', 'status', 'created_at', 'updated_at')
    list_filter = ('src', 'status')


class GuestEntranceControlInfoAdmin(admin.ModelAdmin):
    list_display = ('platform', 'min_adr', 'max_adr', 'min_ios', 'max_ios', 'src', 'status', 'created_at', 'updated_at')
    list_filter = ('src', 'status')

    def save_model(self, request, obj, form, change):
        obj.min_adr = strip(obj.min_adr)
        obj.max_adr = strip(obj.max_adr)
        obj.min_ios = strip(obj.min_ios)
        obj.max_ios = strip(obj.max_ios)
        obj.save()

        # 设置游客入口控制
        set_guest_entrance_control(obj)

    def delete_model(self, request, obj):
        obj.delete()

        # 删除游客入口控制
        delete_guest_entrance_control()


class BoxProgramVersionInfoAdmin(admin.ModelAdmin):
    list_display = ('srv_version_code', 'srv_version_name', 'srv_sha1', 'proc_version_code', 'proc_version_name', 'proc_sha1', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )

    def save_model(self, request, obj, form, change):
        obj.srv_version_name = strip(obj.srv_version_name)
        obj.srv_sha1 = strip(obj.srv_sha1)
        obj.proc_version_name = strip(obj.proc_version_name)
        obj.proc_sha1 = strip(obj.proc_sha1)
        obj.save()

        # 设置 BOX 程序版本信息
        set_box_program_version()

    def delete_model(self, request, obj):
        obj.delete()

        # 设置 BOX 程序版本信息
        set_box_program_version()


admin.site.register(LatestAppInfo, LatestAppInfoAdmin)
admin.site.register(PatchInfo, PatchInfoAdmin)
admin.site.register(APPSettingsInfo, APPSettingsInfoAdmin)
admin.site.register(SplashInfo, SplashInfoAdmin)
admin.site.register(FeedbackInfo, FeedbackInfoAdmin)
admin.site.register(GuestEntranceControlInfo, GuestEntranceControlInfoAdmin)
admin.site.register(BoxProgramVersionInfo, BoxProgramVersionInfoAdmin)
