#!/usr/bin/env python3
import os
import re
from pathlib import Path
from typing import List

BASE_URL = "https://huggingface.co/csukuangfj/sherpa-onnx-apk/resolve/main/"

from dataclasses import dataclass


@dataclass
class APK:
    major: int
    minor: int
    patch: int
    arch: str
    short_name: str

    def __init__(self, s):
        # sherpa-onnx-1.9.23-arm64-v8a-kws-en-gigaspeech-zipformer.apk
        # sherpa-onnx-1.9.23-x86-kws-en-gigaspeech-zipformer.apk
        s = str(s).split("/")[-1]
        split = s.split("-")
        self.major, self.minor, self.patch = list(map(int, split[2].split(".")))
        self.arch = split[3]
        self.lang = split[5]
        self.short_name = split[6] + split[7]
        if "arm" in s:
            self.arch += "-" + split[4]
            self.lang = split[6]
            self.short_name = split[7] + split[8]

        if "armeabi" in self.arch:
            self.arch = "y" + self.arch

        if "arm64" in self.arch:
            self.arch = "z" + self.arch


def sort_by_apk(x):
    x = APK(x)
    return (x.major, x.minor, x.patch, x.arch, x.lang, x.short_name)


def get_all_files(d_list: List[str], suffix: str) -> List[str]:
    if isinstance(d_list, str):
        d_list = [d_list]

    min_major = 1
    min_minor = 9
    min_patch = 10

    ss = []
    for d in d_list:
        for root, _, files in os.walk(d):
            for f in files:
                if f.endswith(suffix):
                    major, minor, patch = list(map(int, f.split("-")[2].split(".")))
                    if major >= min_major and minor >= min_minor and patch >= min_patch:
                        ss.append(os.path.join(root, f))

    ans = sorted(ss, key=sort_by_apk, reverse=True)

    return list(map(lambda x: BASE_URL + str(x), ans))


def to_file(filename: str, files: List[str]):
    content = r"""
<h1> APKs for keyword spotting </h1>
This page lists the <strong>keyword spotting</strong> APKs for <a href="http://github.com/k2-fsa/sherpa-onnx">sherpa-onnx</a>,
one of the deployment frameworks of <a href="https://github.com/k2-fsa">the Next-gen Kaldi project</a>.
<br/>
The name of an APK has the following rule:
<ul>
 <li> sherpa-onnx-{version}-{arch}-kws-${lang}-{dataset}-{model}.apk
</ul>
where
<ul>
 <li> version: It specifies the current version, e.g., 1.9.23
 <li> arch: The architecture targeted by this APK, e.g., arm64-v8a, armeabi-v7a, x86_64, x86
 <li> lang: The lang of the model used in the APK, e.g., en for English, zh for Chinese
 <li> dataset: The dataset used to train the model
 <li> model: The name of the model used in the APK
</ul>

<br/>

You can download all supported models from
<a href="https://github.com/k2-fsa/sherpa-onnx/releases/tag/kws-models">https://github.com/k2-fsa/sherpa-onnx/releases/tag/kws-models</a>

<br/>
<br/>

<strong>Note about the license</strong> The code of Next-gen Kaldi is using
<a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0 license</a>. However,
we support models from different frameworks. Please check the license of your selected model.

<br/>
<br/>

<!--
see https://www.tablesgenerator.com/html_tables#
-->

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
<thead>
  <tr>
    <th class="tg-0pky">APK</th>
    <th class="tg-0lax">Comment</th>
    <th class="tg-0pky">Model</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td class="tg-0pky">sherpa-onnx-x.y.z-arm64-v8a-kws-zh-wenetspeech-zipformer.apk</td>
    <td class="tg-0lax">It supports only Chinese.</td>
    <td class="tg-0pky"><a href="https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.tar.bz2">sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.tar.bz2</a></td>
  </tr>
  <tr>
    <td class="tg-0pky">sherpa-onnx-x.y.z-arm64-v8a-kws-en-gigaspeech-zipformer.apk</td>
    <td class="tg-0lax">It supports only English.</td>
    <td class="tg-0pky"><a href="https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2">sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2</a></td>
  </tr>
</tbody>
</table>

<br/>
<br/>

<div/>
    """
    if "-cn" not in filename:
        content += """
        For Chinese users, please <a href="./apk-cn.html">visit this address</a>,
        which replaces <a href="huggingface.co">huggingface.co</a> with <a href="hf-mirror.com">hf-mirror.com</a>
        <br/>
        <br/>
        中国用户, 请访问<a href="./apk-cn.html">这个地址</a>
        <br/>
        <br/>
        """

    with open(filename, "w") as f:
        print(content, file=f)
        for x in files:
            name = x.rsplit("/", maxsplit=1)[-1]
            print(f'<a href="{x}" />{name}<br/>', file=f)


def main():
    apk = get_all_files("kws", suffix=".apk")
    to_file("./apk-kws.html", apk)

    # for Chinese users
    apk2 = []
    for a in apk:
        a = a.replace("huggingface.co", "hf-mirror.com")
        a = a.replace("resolve", "blob")
        apk2.append(a)

    to_file("./apk-kws-cn.html", apk2)


if __name__ == "__main__":
    main()