package com.tencent.mm.plugin.sight.decode.a;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build.VERSION;
import android.os.Looper;
import android.view.Surface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.util.d;
import com.tencent.mm.modelvideo.o;
import com.tencent.mm.plugin.sight.base.SightVideoJNI;
import com.tencent.mm.sdk.b.c;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.xwalk.core.XWalkFeature;

public abstract class b {
    private static HashMap<String, WeakReference<Bitmap>> qxK = new HashMap();
    private static Map<String, Integer> qxO = new ConcurrentHashMap();
    private boolean fNO = false;
    public boolean fVc = true;
    public String ien = BuildConfig.FLAVOR;
    private ak lda;
    private Surface mSurface;
    public int position;
    private volatile h qxA;
    private volatile b qxB;
    public volatile i qxC;
    private volatile c qxD;
    private k qxE;
    private j qxF;
    private d qxG;
    public boolean qxH = true;
    public boolean qxI = true;
    public boolean qxJ = false;
    public boolean qxL = false;
    public double qxM = -1.0d;
    boolean qxN = false;
    public double qxP = -1.0d;
    private boolean qxQ = false;
    public boolean qxR = false;
    private a qxS;
    public volatile e qxT;
    public volatile f qxU;
    public volatile g qxV;
    private int qxl = 0;
    private int qxm = 0;
    private String qxn = BuildConfig.FLAVOR;
    private int qxo = -1;
    private Queue<Integer> qxp = new ConcurrentLinkedQueue();
    protected int qxq = 41;
    private Bitmap qxr;
    public Bitmap qxs;
    private Bitmap qxt;
    private WeakReference<View> qxu;
    private WeakReference<TextView> qxv;
    private long qxw;
    public WeakReference<View> qxx;
    private Animation qxy;
    private Animation qxz;

    public abstract void U(Bitmap bitmap);

    public abstract void dH(int i, int i2);

    public static Bitmap b(Context context, int i, int i2, int i3, int i4) {
        String format = String.format("%s-%s-%s-%s", new Object[]{Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), Integer.valueOf(i4)});
        WeakReference weakReference = (WeakReference) qxK.get(format);
        if (weakReference != null && weakReference.get() != null) {
            return (Bitmap) weakReference.get();
        }
        if (context == null) {
            ab.w("MicroMsg.SightPlayController", "get mask bmp, but context is null");
            return null;
        } else if (i <= 0) {
            ab.w("MicroMsg.SightPlayController", "get mask bmp, but mask id error");
            return null;
        } else if (i2 <= 0 || i4 <= 0 || i3 <= 0) {
            ab.w("MicroMsg.SightPlayController", "get mask bmp, but size error");
            return null;
        } else {
            long yz = bo.yz();
            int i5 = (i2 * i4) / i3;
            NinePatchDrawable ninePatchDrawable = (NinePatchDrawable) context.getResources().getDrawable(i);
            ninePatchDrawable.setBounds(0, 0, i2, i5);
            Bitmap createBitmap = Bitmap.createBitmap(i2, i5, Config.ARGB_8888);
            ninePatchDrawable.draw(new Canvas(createBitmap));
            qxK.put(format, new WeakReference(createBitmap));
            ab.d("MicroMsg.SightPlayController", "create mask bmp use %dms", new Object[]{Long.valueOf(bo.az(yz))});
            return createBitmap;
        }
    }

    public static void WY() {
        o.i(new Runnable() {
            public final void run() {
                AppMethodBeat.i(70226);
                long currentTimeMillis = System.currentTimeMillis();
                try {
                    SightVideoJNI.freeAll();
                } catch (Exception e) {
                    ab.e("MicroMsg.SightPlayController", "free all sight error");
                    ab.printErrStackTrace("MicroMsg.SightPlayController", e, BuildConfig.FLAVOR, new Object[0]);
                }
                ab.i("MicroMsg.SightPlayController", "free all, use %d ms", new Object[]{Long.valueOf(System.currentTimeMillis() - currentTimeMillis)});
                AppMethodBeat.o(70226);
            }
        }, 0);
    }

    public b(int i, View view) {
        this.qxm = i;
        this.lda = new ak(Looper.getMainLooper());
        this.qxx = new WeakReference(view);
        ab.i("MicroMsg.SightPlayController", "new SightPlayController, drawType %d", new Object[]{Integer.valueOf(i)});
    }

    /* Access modifiers changed, original: protected */
    public int ckZ() {
        return -1;
    }

    public final void kz(boolean z) {
        if (this.qxA != null) {
            o.y(this.qxA);
            this.qxA.ovl = true;
        }
        if (this.qxD != null) {
            this.lda.removeCallbacks(this.qxD);
            this.qxD.ovl = true;
        }
        if (this.qxB != null) {
            o.y(this.qxB);
            this.qxB.ovl = true;
        }
        if (this.qxC != null) {
            this.qxC.type = z ? 0 : 2;
            o.i(this.qxC, 0);
        }
    }

    public final boolean cla() {
        if (1 == this.qxm) {
            if (this.qxB == null || this.qxB.ovl) {
                return false;
            }
            return true;
        } else if (this.qxD == null || this.qxD.ovl || this.qxB == null || this.qxB.ovl) {
            return false;
        } else {
            return true;
        }
    }

    public final void restart() {
        ab.i("MicroMsg.SightPlayController", "#0x%x restart, canPlay %B, videoPath %s, videoId %d", new Object[]{Integer.valueOf(hashCode()), Boolean.valueOf(this.qxI), this.ien, Integer.valueOf(this.qxo)});
        if (!this.qxI) {
            clear();
        } else if (cla()) {
            ab.w("MicroMsg.SightPlayController", "#0x%x is runing, do nothing when restart request asked, videoPath %s", new Object[]{Integer.valueOf(hashCode()), this.ien});
        } else {
            boolean z;
            if (this.qxo < 0) {
                z = true;
            } else {
                z = false;
            }
            kz(z);
            this.qxw = 0;
            if (clb()) {
                ab.e("MicroMsg.SightPlayController", "#0x%x is bad fps, do nothing when restart", new Object[]{Integer.valueOf(hashCode())});
            } else if (this.qxo < 0) {
                ab.w("MicroMsg.SightPlayController", "#0x%x restart match error video id, try reopen video, videoPath %s", new Object[]{Integer.valueOf(hashCode()), this.ien});
                if (!bo.isNullOrNil(this.ien)) {
                    if (WS(this.ien)) {
                        this.qxA = new h(this, (byte) 0);
                        o.i(this.qxA, 0);
                        return;
                    }
                    ab.w("MicroMsg.SightPlayController", "Check Sight Fail!!! return");
                    clear();
                }
            } else if (1 == this.qxm) {
                this.qxB = new b(this, (byte) 0);
                this.qxD = null;
                o.i(this.qxB, 0);
            } else {
                this.qxB = new b(this, (byte) 0);
                this.qxD = new c(this);
                this.qxB.qyb = this.qxD;
                this.qxD.qye = this.qxB;
                o.i(this.qxB, 0);
            }
        }
    }

    public final void clear() {
        ab.i("MicroMsg.SightPlayController", "#0x%x do clear, remove render job, video id %d, runing %B", new Object[]{Integer.valueOf(hashCode()), Integer.valueOf(this.qxo), Boolean.valueOf(cla())});
        kz(true);
        this.qxw = 0;
        d(this.qxp);
        this.qxo = -1;
        this.ien = BuildConfig.FLAVOR;
        this.qxn = "ERROR#PATH";
        this.qxt = null;
        this.qxM = 0.0d;
        this.qxN = false;
        com.tencent.mm.memory.o.fdT.f(this.qxr);
        this.qxr = null;
    }

    private void d(Queue<Integer> queue) {
        while (true) {
            Integer num = (Integer) queue.poll();
            if (num != null) {
                CE(num.intValue());
            } else {
                return;
            }
        }
    }

    private void CE(int i) {
        o.i(new 2(this, i), 0);
    }

    public final void bf(String str, boolean z) {
        ab.i("MicroMsg.SightPlayController", "#0x%x data: set video[%s], old path[%s], fling[%B], last video id %d, recording %B, canPlay %B", new Object[]{Integer.valueOf(hashCode()), str, this.ien, Boolean.valueOf(z), Integer.valueOf(this.qxo), Boolean.valueOf(this.qxR), Boolean.valueOf(this.qxI)});
        if (this.qxR) {
            kz(false);
        } else if (clb()) {
            ab.e("MicroMsg.SightPlayController", "is bad fps, do nothing when set video path");
            clear();
        } else if (!this.qxI) {
            clear();
        } else if (z) {
            this.qxn = str;
            kz(false);
        } else if (this.ien.equals(str)) {
            this.qxn = "ERROR#PATH";
            kz(false);
            restart();
        } else {
            clear();
            if (str == null) {
                str = BuildConfig.FLAVOR;
            }
            this.ien = str;
            if (bo.isNullOrNil(this.ien)) {
                ab.w("MicroMsg.SightPlayController", "empty video path, do draw empty thumb and return");
                T(null);
            } else if (WS(this.ien)) {
                this.qxA = new h(this, (byte) 0);
                o.i(this.qxA, 0);
            } else {
                ab.w("MicroMsg.SightPlayController", "Check Sight Fail!!! return");
                clear();
            }
        }
    }

    public static boolean WS(String str) {
        if (bo.isNullOrNil(str)) {
            return false;
        }
        Integer num = (Integer) qxO.get(str);
        if (num == null || 2 != num.intValue()) {
            return true;
        }
        return false;
    }

    public final boolean clb() {
        if (this.qxL) {
            return false;
        }
        if (VERSION.SDK_INT >= 11) {
            if (this.qxl < 3) {
                return false;
            }
            ab.v("MicroMsg.SightPlayController", "match not check bad fps, but now is bad fps");
            this.qxl = 0;
            return false;
        } else if (this.qxl >= 3) {
            return true;
        } else {
            return false;
        }
    }

    /* Access modifiers changed, original: protected|final */
    public final void clc() {
        this.qxq = XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION / Math.max(1, (int) SightVideoJNI.getVideoRate(this.qxo));
        ab.d("MicroMsg.SightPlayController", "#0x%x update video rate to %d fps, delay %d ms", new Object[]{Integer.valueOf(hashCode()), Integer.valueOf(r0), Integer.valueOf(this.qxq)});
    }

    public final void g(Surface surface) {
        ab.v("MicroMsg.SightPlayController", "set play surface %s", new Object[]{surface});
        this.mSurface = surface;
        o.i(this.qxE, 0);
    }

    public final void setThumbBgView(View view) {
        this.qxu = new WeakReference(view);
    }

    public final void setSightInfoView(TextView textView) {
        this.qxv = new WeakReference(textView);
    }

    public final void T(Bitmap bitmap) {
        boolean z = true;
        String str = "MicroMsg.SightPlayController";
        String str2 = "draw surface thumb, thumb is null ? %B";
        Object[] objArr = new Object[1];
        if (bitmap != null) {
            z = false;
        }
        objArr[0] = Boolean.valueOf(z);
        ab.v(str, str2, objArr);
        o.y(this.qxE);
        if (this.qxE == null) {
            this.qxE = new k(this, (byte) 0);
        }
        this.qxE.qyh = new WeakReference(bitmap);
        o.i(this.qxE, 0);
    }

    public final c cld() {
        if (this.qxS == null) {
            this.qxS = new a(this);
        }
        return this.qxS;
    }

    public final double cle() {
        if (this.qxo == -1) {
            return 0.0d;
        }
        return SightVideoJNI.getVideoDuration(this.qxo);
    }

    static /* synthetic */ void a(b bVar, int i, int i2) {
        ab.d("MicroMsg.SightPlayController", "#0x%x check bmp, video width %d, height %d", new Object[]{Integer.valueOf(bVar.hashCode()), Integer.valueOf(i), Integer.valueOf(i2)});
        if (bVar.qxr == null) {
            bVar.qxr = com.tencent.mm.memory.o.fdT.a(new com.tencent.mm.memory.o.b(i, i2));
            ab.b("MicroMsg.SightPlayController", "checkBmp, create new one, videoPath: %s", new Object[]{bVar.ien});
        } else if (bVar.qxr.getWidth() != i || bVar.qxr.getHeight() != i2) {
            int i3;
            ab.w("MicroMsg.SightPlayController", "reset bmp, old value " + bVar.qxr.getWidth() + "*" + bVar.qxr.getHeight());
            if (!d.iW(19) || bVar.qxr.getAllocationByteCount() < (i2 * i) * 4) {
                i3 = 0;
            } else {
                try {
                    ab.w("MicroMsg.SightPlayController", "reset bmp, try directly reconfigure");
                    bVar.qxr.reconfigure(i, i2, Config.ARGB_8888);
                    i3 = 1;
                } catch (Exception e) {
                    ab.e("MicroMsg.SightPlayController", "reconfigure failed: %s" + e.getMessage());
                    i3 = 0;
                }
            }
            if (i3 == 0) {
                com.tencent.mm.memory.o.fdT.f(bVar.qxr);
                bVar.qxr = com.tencent.mm.memory.o.fdT.a(new com.tencent.mm.memory.o.b(i, i2));
            }
            ab.b("MicroMsg.SightPlayController", "checkBmp, the origin bmp size not match, create new one, videoPath: %s", new Object[]{bVar.ien});
        }
    }

    static /* synthetic */ void B(b bVar) {
        if (bVar.qxx != null && -1 != bVar.ckZ() && bVar.qxH) {
            if (bVar.qxy == null && bVar.qxx.get() != null) {
                bVar.qxy = AnimationUtils.loadAnimation(((View) bVar.qxx.get()).getContext(), bVar.ckZ());
            }
            if (bVar.qxG == null) {
                bVar.qxG = new d(bVar, (byte) 0);
            }
            bVar.lda.post(bVar.qxG);
        }
    }
}
