package com.xixun.xixunplayer;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Choreographer;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

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

@SuppressLint("ViewConstructor")
public class SrcTimer extends View implements Choreographer.FrameCallback {

    HashMap<String, Bitmap> imgMap = new HashMap<>();
    ArrayList<Bitmap> imgs = new ArrayList<>();
    Bitmap text, day, hour, min, sec;
    int spaceWidth, len;
    Paint paint = new Paint();
    long targetTime;
    boolean isDown;
    boolean isMultiline;
    boolean hasDay, hasHour, hasMin, hasSec;

    public SrcTimer(Prog prog, JSMap json) {
        super(prog.getContext());
        var imgEntrys = json.jsmap("imgs").entrySet();
        for(var imgEntry : imgEntrys) imgMap.put(imgEntry.getKey(), BitmapFactory.decodeFile(Util.programDir+"/"+imgEntry.getValue()));
        text = imgMap.get("text");
        day = imgMap.get("day");
        hour = imgMap.get("hour");
        min = imgMap.get("min");
        sec = imgMap.get("sec");
        spaceWidth = (int) Math.round(json.dbl("spaceWidth"));
        isDown = json.bool("isDown");
        var dateFmt = new SimpleDateFormat("y-M-d H:m:s");
        try {
            targetTime = dateFmt.parse(json.stnn("targetTime")).getTime() / 1000;
        } catch (Exception e) {
            Util.makeText(prog.getContext(), Util.toStr(e)).show();
            Util.printStackTrace(e);
        }
        hasDay = json.bool("hasDay");
        hasHour = json.bool("hasHour");
        hasMin = json.bool("hasMin");
        hasSec = json.bool("hasSec");
        isMultiline = json.bool("isMultiline");
        paint.setTextAlign(Paint.Align.CENTER);
        try {
            setBackgroundColor(Color.parseColor(json.stnn("backColor")));
        } catch (Exception ignored) {}
        prog.calls.add(this);
    }

    void cal() {
        var secs = isDown ? targetTime - lastSec : lastSec - targetTime;
        if(secs < 0) secs = 0;
        len = 0;
        imgs.clear();
        if(text!=null && ! isMultiline) {
            imgs.add(text);
            imgs.add(null);
            len += text.getWidth();
            len += spaceWidth;
        }
        if(hasDay) {
            var str = Long.toString(secs/86400);
            for(int cc=0; cc<str.length(); cc++) {
                var img = imgMap.get(str.substring(cc, cc+1));
                imgs.add(img);
                len += img.getWidth();
            }
            imgs.add(null);
            imgs.add(day);
            imgs.add(null);
            len += day.getWidth();
            len += spaceWidth * 2;
            secs %= 86400;
        }
        if(hasHour) {
            var str = NumFmts.zz().format(secs/3600);
            for(int cc=0; cc<str.length(); cc++) {
                var img = imgMap.get(str.substring(cc, cc+1));
                imgs.add(img);
                len += img.getWidth();
            }
            imgs.add(hour);
            imgs.add(null);
            len += hour.getWidth();
            len += spaceWidth;
            secs %= 3600;
        }
        if(hasMin) {
            var str = NumFmts.zz().format(secs/60);
            for(int cc=0; cc<str.length(); cc++) {
                var img = imgMap.get(str.substring(cc, cc+1));
                imgs.add(img);
                len += img.getWidth();
            }
            imgs.add(min);
            imgs.add(null);
            len += min.getWidth();
            len += spaceWidth;
            secs %= 60;
        }
        if(hasSec) {
            var str = NumFmts.zz().format(secs);
            for(int cc=0; cc<str.length(); cc++) {
                var img = imgMap.get(str.substring(cc, cc+1));
                imgs.add(img);
                len += img.getWidth();
            }
            imgs.add(sec);
            imgs.add(null);
            len += sec.getWidth();
            len += spaceWidth;
        }
        imgs.remove(imgs.size()-1);
        len -= spaceWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = (getWidth()-len) / 2, y;
        if(text!=null && isMultiline) {
            y = getHeight() / 2;
            canvas.drawBitmap(text, (getWidth()-text.getWidth()) / 2, y-text.getHeight(), null);
        } else y = (getHeight() - imgs.get(0).getHeight()) / 2;
        for(var img : imgs) {
            if(img==null) x += spaceWidth;
            else {
                canvas.drawBitmap(img, x, y, null);
                x += img.getWidth();
            }
        }
    }

    long lastSec;

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