#!/usr/bin/env python
# -*- coding: utf-8 -*-
import webview
import random
import time
from flask import Flask, render_template, jsonify, request

server = Flask(__name__, template_folder='./templates', static_folder='./static')


@server.route("/")
def index():
    return render_template("search-container.html")


@server.route("/GetSearchURL")
def get_search_url():
    search = request.args.get('search', '')
    platform = request.args.get('platform', '')
    # 使用时间戳生成随机且绝不重复的3位数ID
    timestamp = int(time.time())  # 获取当前时间戳
    random.seed(timestamp)  # 设置随机数种子为时间戳
    random_id = random.randint(100, 999)  # 生成一个3位随机整数作为ID
    if platform != "":
        dynamic_url = f'/SearchPage?search={search}&platform={platform}'
        data = {
            'code': 0,
            'msg': 'succ',
            'data': {
                'id': random_id,
                'title': search,
                'search_url': dynamic_url
            }
        }
        return jsonify(data)
    else:
        return jsonify({'code': 1, 'msg': 'error'})


@server.route("/SearchPage")
def search_page():
    search = request.args.get('search', '')
    platform = request.args.get('platform', '')
    data = {"title": search}
    return render_template("iframe-search-results.html", render_data=data)


@server.route("/ComicDetails/<platform>/<comic>")
def comic_details_page(platform, comic):
    print(platform, comic)
    data = {"comic": comic}
    return render_template("iframe-comic-details.html", render_data=data)


if __name__ == '__main__':
    chinese = {
        'global.quitConfirmation': u'确定关闭?',
    }

    window = webview.create_window(
        title='IOmga',
        url=server,
        width=1080,
        height=845,
        resizable=True,
        # frameless=True,
        # easy_drag=True,
        # hidden=True,
        # transparent=True,  # 窗口透明度
        confirm_close=True,
        text_select=True,
    )
    # edgechromium cef
    webview.start(gui="cef", localization=chinese, debug=True, http_server=True)
