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

from django.conf import settings
from django.contrib import admin
from django.template.loader import render_to_string

from operation.models import APPSettingsInfo, FeedbackInfo, GuestEntranceControlInfo, LatestAppInfo, 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.rsettings import del_app_settings_info, set_app_settings_info
from utils.strip_utils import tstrip


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')

    def save_model(self, request, obj, form, change):
        obj.latest_adr_version_code = tstrip(obj.latest_adr_version_code)
        obj.latest_adr_version_name = tstrip(obj.latest_adr_version_name)
        obj.latest_adr_app = tstrip(obj.latest_adr_app)
        obj.latest_ios_version_code = tstrip(obj.latest_ios_version_code)
        obj.latest_ios_version_name = tstrip(obj.latest_ios_version_name)
        obj.latest_ios_url = tstrip(obj.latest_ios_url)
        obj.save()

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

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

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


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 = tstrip(obj.channel)
        obj.version = tstrip(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(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', 'status', 'created_at', 'updated_at')

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

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

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

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


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