package com.xixun.xixunplayer;

import static android.graphics.Paint.Join.ROUND;
import static android.graphics.Paint.Style.FILL_AND_STROKE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.Choreographer;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.Calendar;
import java.util.TimeZone;

import gnph.util.JSMap;

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

    TimeZone timeZone;
    Bitmap img;
    Rect imgRect;
    int pinHourColor, pinMinColor, pinSecColor;
    Path hPath, mPath, sPath;
    float hAngle, mAngle, sAngle;
    Paint paint = new Paint();
    boolean showSecHand;

    public EleAnaClock(float w, float h, String path, JSMap source, Context context) {
        super(context);
        var timeZoneStr = source.str("timeZone");
        //if(timeZoneStr!=null) timeZone = ZoneId.of(timeZoneStr);
        if(timeZoneStr!=null) timeZone = TimeZone.getTimeZone(timeZoneStr);

        var sideLen = Math.min(w, h);
        var halfSide = sideLen / 2;
        var lineWidth = sideLen / 128;
        if(lineWidth < 1) lineWidth = 1;

        paint.setAntiAlias(true);
        paint.setStyle(FILL_AND_STROKE);
        paint.setStrokeWidth(lineWidth);
        paint.setStrokeJoin(ROUND);

        img = BitmapFactory.decodeFile(path);
        if(img==null) {
            img = Bitmap.createBitmap((int)w, (int)h, Bitmap.Config.ARGB_8888);
            var canvas = new Canvas(img);
            canvas.translate(w / 2, h / 2);
            if(source.bool("showBg")) {
                var bgColor = Color.parseColor(source.stnn("bgColor"));
                var opacity = source.dbl("opacity", -1);
                if(opacity!=-1) bgColor = bgColor & 0x00ffffff | ((int)opacity*255)<<24;
                paint.setColor(bgColor);
                canvas.drawOval(-halfSide, -halfSide, halfSide, halfSide, paint);
            }
            var minMarkSize = 2.5f;
            var hourMarkSize = 5f;
            var scaleHourColor = Color.parseColor(source.stnn("scaleHourColor"));
            var scaleMinColor = Color.parseColor(source.stnn("scaleMinColor"));
            var r = (sideLen - Math.max(minMarkSize, hourMarkSize)) / 2;
            for(int i=0; i<60; i++) {
                var k = i * Math.PI / 30;
                var x = (float) (Math.sin(k) * r);
                var y = (float) (-Math.cos(k) * r);
                if(i % 5 > 0) {
                    var rr = minMarkSize / 2f;
                    paint.setColor(scaleMinColor);
                    canvas.drawOval(x-rr, y-rr, x+rr, y+rr, paint);
                } else {
                    var rr = hourMarkSize / 2f;
                    paint.setColor(scaleHourColor);
                    canvas.drawOval(x-rr, y-rr, x+rr, y+rr, paint);
                    //if(m_attr.hourMark==2) drawMarkNumber(painter, {x, y}, m_attr.hourMarkColor, m_attr.hourMarkSize, i/5);
                }
            }
        }
        imgRect = new Rect(0, 0, img.getWidth(), img.getHeight());
        pinHourColor = Color.parseColor(source.stnn("pinHourColor"));
        pinMinColor = Color.parseColor(source.stnn("pinMinColor"));
        pinSecColor = Color.parseColor(source.stnn("pinSecColor"));
        var hhLen = (float) source.dbl("pinHourLen", 50);
        var mhLen = (float) source.dbl("pinMinLen", 75);
        var shLen = (float) source.dbl("pinSecLen", 100);
        var hhWidth = (float) source.dbl("pinHourWidth", 15);
        var mhWidth = (float) source.dbl("pinMinWidth", 10);
        var shWidth = (float) source.dbl("pinSecWidth", 5);
        showSecHand = source.bool("showSecond");

        var rx = hhWidth*sideLen/400;
        hPath = new Path();
        hPath.moveTo(rx, 0);
        hPath.lineTo(0, hhLen*sideLen/-200);
        hPath.lineTo(-rx, 0);
        hPath.close();

        rx = mhWidth*sideLen/400;
        mPath = new Path();
        mPath.moveTo(rx, 0);
        mPath.lineTo(0, mhLen*sideLen/-200);
        mPath.lineTo(-rx, 0);
        mPath.close();
        rx = shWidth*sideLen/400;
        sPath = new Path();
        sPath.moveTo(rx, 0);
        sPath.lineTo(0, shLen*sideLen/-200);
        sPath.lineTo(-rx, 0);
        sPath.close();
    }
    void cal() {
        //var time = timeZone==null ? LocalTime.now() : LocalTime.now(timeZone);
        var calendar = timeZone==null ? Calendar.getInstance() : Calendar.getInstance(timeZone);
        sAngle = calendar.get(Calendar.SECOND) * 6;
        mAngle = calendar.get(Calendar.MINUTE) * 6 + sAngle/60;
        hAngle = calendar.get(Calendar.HOUR_OF_DAY) * 30 + mAngle/12;
    }
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if(lastSec==0) {
            cal();
            choreographer.postFrameCallback(this);
            lastSec = 1;
        }
        if(img != null) canvas.drawBitmap(img, imgRect, new RectF(0,0, getWidth(), getHeight()), paint);
        canvas.translate(getWidth()/2f, getHeight()/2f);

        paint.setColor(pinHourColor);
        canvas.rotate(hAngle);
        canvas.drawPath(hPath, paint);

        paint.setColor(pinMinColor);
        canvas.rotate(mAngle-hAngle);
        canvas.drawPath(mPath, paint);
        if(showSecHand) {
            paint.setColor(pinSecColor);
            canvas.rotate(sAngle-mAngle);
            canvas.drawPath(sPath, paint);
        }
    }

    Choreographer choreographer = Choreographer.getInstance();
    long lastSec;

    @Override
    public void doFrame(long frameTimeNanos) {
        if(! isShown()) {
            lastSec = 0;
            return;
        }
        var sec = System.currentTimeMillis() / 1000;
        if(sec != lastSec) {
            lastSec = sec;
            cal();
            invalidate();
        }
        choreographer.postFrameCallback(this);
    }
}