package com.xixun.xixunplayer;

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

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

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

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

    static DecimalFormat fmt00_0 = NumFmts.newDecFmt("00.0");

    long targetTime;
    String html, lineHeight, prefix, suffix;
    boolean isUp, hasDay, hasHour, hasMin, hasSec, hasFPS;

    public SrcCountdown(Prog prog, JSMap json) {
        super(prog.getContext());
        try {
            var backColor = json.str("backColor");
            setBackgroundColor(backColor==null ? Color.TRANSPARENT : Color.parseColor(backColor));
        } catch (Exception ignored) {
            setBackgroundColor(Color.TRANSPARENT);
        }
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        setInitialScale(100);
        html = json.stnn("html");
        hasFPS = html.contains("%FPS");
        isUp = json.bool("isUp");
        lineHeight = json.str("lineHeight");
        prefix = "<body style=\"color:#fff;margin:0;padding:0;";
        if(! hasFPS && lineHeight!=null) prefix += "line-height:"+lineHeight+";";
        prefix += "\">";
        suffix = "</body>";
        try {
            var start = html.substring(0, 16).trim();
            if(start.startsWith("<body>")) {
                suffix = "";
                html = html.substring(6);
            } else if(start.startsWith("<html") || start.startsWith("<body") || start.startsWith("<!DOCTYPE")) prefix = suffix = "";
        } catch (Exception ignored) {
        }
        hasDay = html.contains("%d");
        hasHour = html.contains("%h");
        hasMin = html.contains("%m");
        hasSec = html.contains("%s");
        var time = json.stnn("time");
        try {
            try {
                targetTime = new SimpleDateFormat("y-M-d H:m:s").parse(time).getTime() / 1000;
            } catch (Exception e) {
                targetTime = new SimpleDateFormat("y-M-d H:m").parse(time).getTime() / 1000;
            }
        } catch (Exception e) {
            Util.printStackTrace(e);
            Util.makeText(prog.getContext(), Util.toStr(e)).show();
        }
        prog.calls.add(this);
    }

    void cal() {
        var secs = isUp ? lastSec - targetTime : targetTime - lastSec;
        if(secs < 0) secs = 0;
        var htm = html;
        if(hasDay) {
            htm = htm.replace("%d", Long.toString(secs/86400));
            secs %= 86400;
        }
        if(hasHour) {
            htm = htm.replace("%h", NumFmts.zz().format(secs/3600));
            secs %= 3600;
        }
        if(hasMin) {
            htm = htm.replace("%m", NumFmts.zz().format(secs/60));
            secs %= 60;
        }
        if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(secs));
        loadDataWithBaseURL(null, prefix+htm+suffix, "text/html", "UTF-8", null);
    }

    long lastSec, lastMs;

    @Override
    public void doFrame(long ms) {
        if(! isShown()) return;
        if(hasFPS) {
            var fps = 1000.0 / (ms - lastMs);
            lastMs = ms;
            var htm = html;
            htm = htm.replace("%FPS", fmt00_0.format(fps));
            loadDataWithBaseURL(null, prefix+htm+suffix, "text/html", "UTF-8", null);
            return;
        }
        var sec = ms / 1000;
        if(sec != lastSec) {
            lastSec = sec;
            cal();
        }
    }
}