package com.xixun.xixunplayer;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.Choreographer;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.ArrayList;

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

    ArrayList<Bitmap> imgs;
    Rect rect = new Rect();
    int interval, cur, end, step;
    char direct;

    public SrcScroll(Prog prog, ArrayList<Bitmap> imgs, char direct, double speed) {
        super(prog.getContext());
        this.imgs = imgs;
        this.direct = direct;
        var img0 = imgs.get(0);
        Util.println("  SrcScroll img cnt "+imgs.size()+", "+img0.getWidth()+"x"+img0.getHeight()+" direct "+direct+" speed "+speed);
        if(img0.getWidth() > 4096) {
            imgs.clear();
            var rem = img0.getWidth();
            do {
                imgs.add(Bitmap.createBitmap(img0, img0.getWidth()-rem, 0, Math.min(4096, rem), img0.getHeight()));
                rem -= 4096;
            } while (rem>0);
        } else if(img0.getHeight() > 4096) {
            imgs.clear();
            var rem = img0.getHeight();
            do {
                imgs.add(Bitmap.createBitmap(img0, 0, img0.getHeight()-rem, img0.getWidth(), Math.min(4096, rem)));
                rem -= 4096;
            } while (rem>0);
        }

        if(speed==0) return;
        int width = 0, height = 0;
        for(var img : imgs) {
            width += img.getWidth();
            height += img.getHeight();
        }
        if(direct=='l') end = -(width-step);
        else if(direct=='r') end = width-step;
        else if(direct=='t') end = -(height-step);
        else if(direct=='b') end = height-step;
        else direct = 0;
        if(direct==0) return;

        interval = step = 1;
        if(speed > 60) step = (int) Math.round(speed/60);
        else if(speed < 60) interval = (int) Math.round(60/speed);
        prog.calls.add(this);
    }
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if(imgs.isEmpty()) return;
        try {
            drawOther(canvas);
        } catch (Throwable e) {
            setVisibility(GONE);
            imgs.clear();
            Util.printStackTrace(e);
        }
    }
    public void drawOther(Canvas canvas) {
        if(! canvas.getClipBounds(rect)) {
            rect.left = rect.top = 0;
            rect.right = getWidth();
            rect.bottom = getHeight();
        }
        if(direct=='l') {
            int ii = 0, x = cur;
            do {
                if(x > rect.left-imgs.get(ii).getWidth() && x < rect.right) canvas.drawBitmap(imgs.get(ii), x, 0, null);
                x += imgs.get(ii).getWidth();
                if(++ii >= imgs.size()) ii = 0;
            } while(x < rect.right);
        } else if(direct=='r') {
            int ii = imgs.size()-1, x = cur + getWidth();
            boolean con1;
            do {
                x -= imgs.get(ii).getWidth();
                con1 = x > rect.left-imgs.get(ii).getWidth();
                if(con1 && x < rect.right) canvas.drawBitmap(imgs.get(ii), x, 0, null);
                if(--ii < 0) ii = imgs.size()-1;
            } while(con1);
        } else if(direct=='t') {
            int ii = 0, y = cur;
            do {
                if(y > rect.top-imgs.get(ii).getHeight() && y < rect.bottom) canvas.drawBitmap(imgs.get(ii), 0, y, null);
                y += imgs.get(ii).getHeight();
                if(++ii >= imgs.size()) ii = 0;
            } while(y < rect.bottom);
        } else if(direct=='b') {
            int ii = imgs.size()-1, y = cur + getHeight();
            boolean con1;
            do {
                y -= imgs.get(ii).getHeight();
                con1 = y > rect.top-imgs.get(ii).getHeight();
                if(con1 && y < rect.bottom) canvas.drawBitmap(imgs.get(ii), 0, y, null);
                if(--ii < 0) ii = imgs.size()-1;
            } while(con1);
        } else canvas.drawBitmap(imgs.get(0), 0, 0, null);
    }

    int freshCnt;

    @Override
    public void doFrame(long ms) {
        if(! isShown()) {
            freshCnt = cur = 0;
            return;
        }
        if(freshCnt < interval) freshCnt++;
        else {
            freshCnt = 1;
            if(direct=='t' || direct=='l') {
                if(cur <= end) cur -= end;
                else cur -= step;
            } else if(direct=='b' || direct=='r') {
                if(cur >= end) cur -= end;
                else cur += step;
            }
            invalidate();
        }
    }
}