package com.xixun.xixunplayer;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.Choreographer;
import android.webkit.WebView;

import java.util.Calendar;
import java.util.Locale;
import java.util.SimpleTimeZone;

import gnph.util.JSMap;
import gnph.util.NumFmts;
import gnph.util.Txts;

@SuppressLint("ViewConstructor")
public class SrcDigitalClock extends WebView implements Choreographer.FrameCallback {

    String html, lineHeight, prefix;
    Calendar calendar;
    Locale locale;
    boolean hasYear, hasMonthStr, hasMonth, hasDay, hasWeek, hasAP, hasHour, hasHour12, hasMin, hasSec;

    public SrcDigitalClock(Prog prog, JSMap json) {
        super(prog.getContext());
        setBackgroundColor(Color.TRANSPARENT);
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        setInitialScale(100);
        html = json.stnn("html");
        lineHeight = json.str("lineHeight");
        var timezone = json.dbl("timezone", 9999);
        calendar = timezone==9999 ? Calendar.getInstance() : Calendar.getInstance(new SimpleTimeZone((int) (timezone*3600000), ""));
        prefix = "<body style=\"color:#fff;margin:0;padding:0;";
        if(lineHeight!=null) prefix += "line-height:"+lineHeight+";";
        prefix += "\">";
        hasYear = html.contains("%y");
        var hhh = html.replace("%Mw", "");
        hasMonthStr = html.length()!=hhh.length();
        hasMonth = hhh.contains("%M");
        hasDay = html.contains("%d");
        hasWeek = html.contains("%w");
        hasAP = html.contains("%am");
        hasHour = html.contains("%H");
        hasHour12 = html.contains("%h");
        hasMin = html.contains("%m");
        hasSec = html.contains("%s");
        try {
            var lan = Txts.split(json.stnn("language"), "-", 1);
            if(lan.isEmpty()) locale = Locale.getDefault();
            else if(lan.size()==1) locale = lan.get(0).equals("cn") ? new Locale("zh", "CN") : new Locale(lan.get(0));
            else locale = new Locale(lan.get(0), lan.get(1));
        } catch (Exception e) {
            Util.printStackTrace(e);
            Util.makeText(prog.getContext(), Util.toStr(e)).show();
        }
        prog.calls.add(this);
    }

    void cal() {
        calendar.setTimeInMillis(lastSec*1000);
        var htm = html;
        if(hasYear) htm = htm.replace("%y", Long.toString(calendar.get(Calendar.YEAR)));
        try {
            if(hasMonthStr) htm = htm.replace("%Mw", calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale));
            if(hasWeek) htm = htm.replace("%w", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, locale));
            if(hasAP) htm = htm.replace("%am", calendar.getDisplayName(Calendar.AM_PM, Calendar.SHORT, locale));
        } catch (Exception ignored) {}
        if(hasMonth) htm = htm.replace("%M", NumFmts.zz().format(calendar.get(Calendar.MONTH)+1));
        if(hasDay) htm = htm.replace("%d", NumFmts.zz().format(calendar.get(Calendar.DAY_OF_MONTH)));
        if(hasHour) htm = htm.replace("%H", NumFmts.zz().format(calendar.get(Calendar.HOUR_OF_DAY)));
        if(hasHour12) htm = htm.replace("%h", NumFmts.zz().format(Util.caseThen(calendar.get(Calendar.HOUR), 0, 12)));
        if(hasMin) htm = htm.replace("%m", NumFmts.zz().format(calendar.get(Calendar.MINUTE)));
        if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(calendar.get(Calendar.SECOND)));
        loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null);
    }

    long lastSec;

    @Override
    public void doFrame(long ms) {
        if(! isShown()) return;
        var sec = ms / 1000;
        if(sec != lastSec) {
            lastSec = sec;
            cal();
        }
    }
}