package com.seewo.factoryaging.exovideo;

import android.content.Context;
import android.os.Environment;
import android.os.FileUtils;
import android.util.Log;
import com.cvte.vman.R;
import com.seewo.factoryaging.newvideo.l;
import com.seewo.factoryaging.newvideo.m;
import h.c.d.c.a;
import j.p0.d.r;
import j.u0.q;
import java.io.File;
import java.io.FileFilter;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import okhttp3.HttpUrl;

/* JADX INFO: compiled from: ExoVideoPresenter.kt */
/* JADX INFO: loaded from: classes.dex */
public final class h extends l {
    private static final String f = Environment.getExternalStorageDirectory().getAbsolutePath();
    private String c = HttpUrl.FRAGMENT_ENCODE_SET;
    private i.a.k.b d;
    private i.a.k.b e;

    private final void n(final boolean z, final String str) {
        this.e = i.a.d.s(Boolean.valueOf(z)).C(i.a.j.b.a.a()).x(new i.a.m.c() { // from class: com.seewo.factoryaging.exovideo.a
            @Override // i.a.m.c
            public final void accept(Object obj) {
                h.o(z, this, str, (Boolean) obj);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void o(boolean z, h hVar, String str, Boolean bool) {
        r.f(hVar, "this$0");
        r.f(str, "$string");
        if (z) {
            m mVarI = hVar.i();
            if (mVarI != null) {
                mVarI.y(str);
                return;
            }
            return;
        }
        Log.d("test", "callbackToMainThread");
        m mVarI2 = hVar.i();
        if (mVarI2 != null) {
            mVarI2.f(str);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean p(h hVar, String str) {
        r.f(hVar, "this$0");
        r.f(str, "it");
        File file = new File(str + File.separator + "test.mp4");
        if (file.exists() && file.length() == 0) {
            Log.d("ExoVideoPresenter", "find  burn video but video size is " + file.length() + " delete it");
            file.delete();
        }
        if (file.exists()) {
            Log.d("ExoVideoPresenter", "find burn video in storage: " + file.getAbsoluteFile());
            String absolutePath = file.getAbsolutePath();
            r.e(absolutePath, "file.absolutePath");
            hVar.n(true, absolutePath);
        }
        return !file.exists();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean q(h hVar, String str) {
        Context contextA;
        r.f(hVar, "this$0");
        r.f(str, "it");
        List<a.n> listF = h.c.d.c.a.d().f();
        Log.i("ExoVideoPresenter", "usb isEmpty:" + listF.isEmpty());
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (i2 >= 3 || !listF.isEmpty()) {
                break;
            }
            try {
                Thread.sleep(1000L);
            } catch (Exception e) {
                Log.w("ExoVideoPresenter", e.toString());
            }
            listF = h.c.d.c.a.d().f();
            i2 = i3;
        }
        if (listF.isEmpty()) {
            m mVarI = hVar.i();
            String string = (mVarI == null || (contextA = mVarI.a()) == null) ? null : contextA.getString(R.string.str_no_burn_video);
            if (string != null) {
                Log.i("ExoVideoPresenter", string);
            }
            r.c(string);
            hVar.n(false, string);
        }
        r.e(listF, "usbItems");
        return !listF.isEmpty();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final i.a.e r(h hVar, String str) {
        Context contextA;
        Context contextA2;
        r.f(hVar, "this$0");
        r.f(str, "it");
        Iterator<a.n> it = h.c.d.c.a.d().f().iterator();
        String string = null;
        File file = null;
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            File[] fileArrListFiles = new File(it.next().a).listFiles(new FileFilter() { // from class: com.seewo.factoryaging.exovideo.g
                @Override // java.io.FileFilter
                public final boolean accept(File file2) {
                    return h.s(file2);
                }
            });
            r.e(fileArrListFiles, "files");
            if (!(fileArrListFiles.length == 0)) {
                int length = fileArrListFiles.length;
                int i2 = 0;
                while (true) {
                    if (i2 >= length) {
                        break;
                    }
                    File file2 = fileArrListFiles[i2];
                    r.e(file2, "file");
                    if (hVar.v(file2)) {
                        file = file2;
                        break;
                    }
                    i2++;
                }
                if (file != null) {
                    break;
                }
            }
        }
        if (file == null) {
            m mVarI = hVar.i();
            if (mVarI != null && (contextA2 = mVarI.a()) != null) {
                string = contextA2.getString(R.string.str_video_no_in_usb);
            }
            throw new IllegalArgumentException(string);
        }
        if (file.length() == 0) {
            Log.d("ExoVideoPresenter", "find  burn video in U disk, but video size is 0 ");
            m mVarI2 = hVar.i();
            if (mVarI2 != null && (contextA = mVarI2.a()) != null) {
                string = contextA.getString(R.string.str_video_size_is_zero);
            }
            throw new IllegalArgumentException(string);
        }
        File file3 = new File(f + File.separator + "test.mp4");
        boolean zCopyFile = FileUtils.copyFile(file, file3);
        String absolutePath = file3.getAbsolutePath();
        r.e(absolutePath, "destFile.absolutePath");
        hVar.c = absolutePath;
        return i.a.d.s(Boolean.valueOf(zCopyFile && file3.exists()));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean s(File file) {
        if (file == null) {
            return false;
        }
        String name = file.getName();
        r.e(name, "p0.name");
        return q.D(name, "test.", false, 2, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void t(h hVar, Boolean bool) {
        Context contextA;
        r.f(hVar, "this$0");
        r.e(bool, "it");
        if (bool.booleanValue()) {
            Log.d("test", "invoke configBurnVideoSucceed");
            m mVarI = hVar.i();
            if (mVarI != null) {
                mVarI.y(hVar.c);
                return;
            }
            return;
        }
        Log.d("test", "copy failed");
        m mVarI2 = hVar.i();
        if (mVarI2 != null) {
            m mVarI3 = hVar.i();
            String string = (mVarI3 == null || (contextA = mVarI3.a()) == null) ? null : contextA.getString(R.string.str_copy_video_failed);
            r.c(string);
            mVarI2.f(string);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void u(h hVar, Throwable th) {
        r.f(hVar, "this$0");
        Log.d("test", "Throwable");
        m mVarI = hVar.i();
        if (mVarI != null) {
            String message = th.getMessage();
            r.c(message);
            mVarI.f(message);
        }
    }

    private final boolean v(File file) {
        String contentTypeFor = URLConnection.getFileNameMap().getContentTypeFor(file.getName());
        if (contentTypeFor != null) {
            return j.u0.r.I(contentTypeFor, "video/", false, 2, null);
        }
        return false;
    }

    @Override // com.seewo.factoryaging.newvideo.l
    public void l() {
        if (com.seewo.factoryaging.s0.h.a.a()) {
            m mVarI = i();
            if (mVarI != null) {
                mVarI.i();
            }
            this.d = i.a.d.s(f).c(c()).k(new i.a.m.e() { // from class: com.seewo.factoryaging.exovideo.b
                @Override // i.a.m.e
                public final boolean test(Object obj) {
                    return h.p(this.a, (String) obj);
                }
            }).k(new i.a.m.e() { // from class: com.seewo.factoryaging.exovideo.c
                @Override // i.a.m.e
                public final boolean test(Object obj) {
                    return h.q(this.a, (String) obj);
                }
            }).l(new i.a.m.d() { // from class: com.seewo.factoryaging.exovideo.e
                @Override // i.a.m.d
                public final Object a(Object obj) {
                    return h.r(this.a, (String) obj);
                }
            }).C(i.a.q.a.c()).u(i.a.j.b.a.a()).y(new i.a.m.c() { // from class: com.seewo.factoryaging.exovideo.d
                @Override // i.a.m.c
                public final void accept(Object obj) {
                    h.t(this.a, (Boolean) obj);
                }
            }, new i.a.m.c() { // from class: com.seewo.factoryaging.exovideo.f
                @Override // i.a.m.c
                public final void accept(Object obj) {
                    h.u(this.a, (Throwable) obj);
                }
            });
            return;
        }
        m mVarI2 = i();
        if (mVarI2 != null) {
            mVarI2.n("Burn Video Not Enabled");
        }
    }

    @Override // com.seewo.factoryaging.newvideo.l
    public void m() {
        i.a.k.b bVar = this.d;
        if (bVar != null && bVar != null) {
            bVar.dispose();
        }
        i.a.k.b bVar2 = this.e;
        if (bVar2 == null || bVar2 == null) {
            return;
        }
        bVar2.dispose();
    }
}
