package com.xixun.xixunplayer;


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

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import gnph.util.JSList;
import gnph.util.JSMap;

public class SrcImage extends View implements Choreographer.FrameCallback {

    static final char effTypes[] = {'l', 't', 'r', 'b'};
    Bitmap img;
    Rect RECT = new Rect();
    RectF rect;
    Paint paint = new Paint();
    int entryDur, exitStart;
    char entryType, exitType;
    boolean isEntryRand, isExitRand;

    public SrcImage(Context context, JSMap map, int w, int h) {
        super(context);
        RECT.right = w;
        RECT.bottom = h;
        img = BitmapFactory.decodeFile(Util.programDir+"/"+map.stnn("id"));
        if(img==null) return;
        rect = new RectF(0, 0, w, h);
//        entryDur = map.intg("entryEffectTimeSpan")*60;
//        if(entryDur > 0) {
//            var effStr = map.str("entryEffect");
//            if(effStr == null || effStr.equalsIgnoreCase("None")) entryDur = 0;
//            else if(effStr.equalsIgnoreCase("moving_left")) entryType = 'l';
//            else if(effStr.equalsIgnoreCase("moving_top")) entryType = 't';
//            else if(effStr.equalsIgnoreCase("moving_right")) entryType = 'r';
//            else if(effStr.equalsIgnoreCase("moving_bottom")) entryType = 'b';
//            else if(effStr.equalsIgnoreCase("alpha_in")) entryType = 'a';
//            else if(effStr.equalsIgnoreCase("zoom_in")) entryType = 'z';
//            else if(effStr.equalsIgnoreCase("Random")) isEntryRand = true;
//            else entryDur = 0;
//        }
//        var exitDur = map.intg("exitEffectTimeSpan")*60;
//        if(exitDur > 0) {
//            var effStrExit = map.str("exitEffect");
//            if(effStrExit == null || effStrExit.equalsIgnoreCase("None")) exitDur = 0;
//            else if(effStrExit.equalsIgnoreCase("moving_left")) exitType = 'l';
//            else if(effStrExit.equalsIgnoreCase("moving_top")) exitType = 't';
//            else if(effStrExit.equalsIgnoreCase("moving_right")) exitType = 'r';
//            else if(effStrExit.equalsIgnoreCase("moving_bottom")) exitType = 'b';
//            else if(effStrExit.equalsIgnoreCase("alpha_out")) exitType = 'a';
//            else if(effStrExit.equalsIgnoreCase("zoom_out")) exitType = 'z';
//            else if(effStrExit.equalsIgnoreCase("Random")) isExitRand = true;
//            else exitDur = 0;
//        }
//        exitStart = exitDur==0 ? Integer.MAX_VALUE : map.intg("timeSpan")*60 - exitDur;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawOther(canvas, this);
    }
    public void drawOther(Canvas canvas, View view) {
//        var fff = ff==0 ? 0 : ff-1;
//        if(fff < entryDur) {
//            if(entryType=='l') {
//                rect.left = RECT.right - fff*RECT.right/(float) entryDur;
//                rect.right = rect.left + RECT.right;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(entryType=='r') {
//                rect.left = -RECT.right + fff*RECT.right/(float) entryDur;
//                rect.right = rect.left + RECT.right;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(entryType=='t') {
//                rect.top = RECT.bottom - fff*RECT.bottom/(float) entryDur;
//                rect.bottom = rect.top + RECT.bottom;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(entryType=='b') {
//                rect.top = -RECT.bottom + fff*RECT.bottom/(float) entryDur;
//                rect.bottom = rect.top + RECT.bottom;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(entryType=='a') {
//                paint.setAlpha(fff*255/entryDur);
//                canvas.drawBitmap(img, null, rect, paint);
//            } else if(entryType=='z') {
//                var haw = RECT.right/2f;
//                var hah = RECT.bottom/2f;
//                var ww = fff*haw/(float) entryDur;
//                var hh = fff*hah/(float) entryDur;
//                rect.left = haw - ww;
//                rect.right = haw + ww;
//                rect.top = hah - hh;
//                rect.bottom = hah + hh;
//                canvas.drawBitmap(img, null, rect, null);
//            }
//        } else if(fff>=exitStart) {
//            fff -= exitStart;
//            if(exitType=='l') {
//                rect.left = - fff*RECT.right/(float) entryDur;
//                rect.right = rect.left + RECT.right;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(exitType=='r') {
//                rect.left = fff*RECT.right/(float) entryDur;
//                rect.right = rect.left + RECT.right;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(exitType=='t') {
//                rect.top = - fff*RECT.bottom/(float) entryDur;
//                rect.bottom = rect.top + RECT.bottom;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(exitType=='b') {
//                rect.top = fff*RECT.bottom/(float) entryDur;
//                rect.bottom = rect.top + RECT.bottom;
//                canvas.drawBitmap(img, null, rect, null);
//            } else if(exitType=='a') {
//                paint.setAlpha(255 - fff*255/entryDur);
//                canvas.drawBitmap(img, null, rect, paint);
//            } else if(exitType=='z') {
//                var haw = RECT.right/2f;
//                var hah = RECT.bottom/2f;
//                var ww = haw - fff*haw/(float) entryDur;
//                var hh = hah - fff*hah/(float) entryDur;
//                rect.left = haw - ww;
//                rect.right = haw + ww;
//                rect.top = hah - hh;
//                rect.bottom = hah + hh;
//                canvas.drawBitmap(img, null, rect, null);
//            }
//        } else
            canvas.drawBitmap(img, null, RECT, null);
    }

    Choreographer choreographer = Choreographer.getInstance();
    int ff;
    boolean canAdd = true;

//    @Override
//    public void onVisibilityAggregated(boolean isVisible) {
//        super.onVisibilityAggregated(isVisible);
//        ff = 0;
//        if(isVisible) {
//            if(canAdd && (entryDur > 0 || exitStart < Integer.MAX_VALUE)) {
//                choreographer.postFrameCallback(this);
//                canAdd = false;
//            }
//        }
//    }
    @Override
    public void doFrame(long frameTimeNanos) {
        if(! isShown()) {
            canAdd = true;
            return;
        }
        if(ff <= entryDur || ff>=exitStart) {
            if(ff==0) {
                if(isEntryRand) entryType = effTypes[Util.rand.nextInt(4)];
                if(isExitRand) exitType = effTypes[Util.rand.nextInt(4)];
            }
            invalidate();
        }
        ff++;
        choreographer.postFrameCallback(this);
        canAdd = false;
    }
}