package com.seewo.factoryaging.newvideo;

import android.content.Context;
import android.os.Environment;
import android.os.FileUtils;
import android.util.Log;
import com.cvte.vman.R;
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: NewVideoPresenter.kt */
/* JADX INFO: loaded from: classes.dex */
public final class n 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.newvideo.j
            @Override // i.a.m.c
            public final void accept(Object obj) {
                n.o(z, this, str, (Boolean) obj);
            }
        });
    }

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

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

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean q(n nVar, String str) {
        Context contextA;
        r.f(nVar, "this$0");
        r.f(str, "it");
        List<a.n> listF = h.c.d.c.a.d().f();
        if (listF.isEmpty()) {
            m mVarI = nVar.i();
            String string = (mVarI == null || (contextA = mVarI.a()) == null) ? null : contextA.getString(R.string.str_no_burn_video);
            if (string != null) {
                Log.i("NewVideoPresenter", string);
            }
            File file = new File("/vendor/app/FactoryAging/test");
            boolean zExists = file.exists();
            Log.d("NewVideoPresenter", "internalFileExists" + zExists);
            if (zExists) {
                String absolutePath = file.getAbsolutePath();
                r.e(absolutePath, "internalFile.absolutePath");
                nVar.n(true, absolutePath);
                return false;
            }
            r.c(string);
            nVar.n(false, string);
        }
        r.e(listF, "usbItems");
        return !listF.isEmpty();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public static final i.a.e r(n nVar, String str) {
        Context contextA;
        Context contextA2;
        r.f(nVar, "this$0");
        r.f(str, "it");
        Iterator<a.n> it = h.c.d.c.a.d().f().iterator();
        String string = null;
        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.newvideo.f
                @Override // java.io.FileFilter
                public final boolean accept(File file2) {
                    return n.s(file2);
                }
            });
            r.e(fileArrListFiles, "files");
            if ((fileArrListFiles.length == 0) == false) {
                int length = fileArrListFiles.length;
                int i2 = 0;
                while (true) {
                    if (i2 >= length) {
                        break;
                    }
                    File file2 = fileArrListFiles[i2];
                    r.e(file2, "file");
                    if (nVar.v(file2)) {
                        file = file2;
                        break;
                    }
                    i2++;
                }
                if (file != null) {
                    break;
                }
            }
        }
        if (file == null) {
            File file3 = new File("/vendor/app/FactoryAging/test");
            if (file3.exists()) {
                String absolutePath = file3.getAbsolutePath();
                r.e(absolutePath, "internalFile.absolutePath");
                nVar.c = absolutePath;
                return i.a.d.s(Boolean.TRUE);
            }
            m mVarI = nVar.i();
            if (mVarI != null && (contextA = mVarI.a()) != null) {
                string = contextA.getString(R.string.str_video_no_in_usb);
            }
            throw new IllegalArgumentException(string);
        }
        File file4 = new File(f + File.separator + "test.mp4");
        m mVarI2 = nVar.i();
        String string2 = (mVarI2 == null || (contextA2 = mVarI2.a()) == null) ? null : contextA2.getString(R.string.str_copy_video_ing);
        r.c(string2);
        nVar.n(false, string2);
        boolean zCopyFile = FileUtils.copyFile(file, file4);
        h.c.d.d.e.a("sync");
        if (zCopyFile) {
            m mVarI3 = nVar.i();
            h.c.a.b.b.c(mVarI3 != null ? mVarI3.a() : null, file4);
        }
        String absolutePath2 = file4.getAbsolutePath();
        r.e(absolutePath2, "destFile.absolutePath");
        nVar.c = absolutePath2;
        return i.a.d.s(Boolean.valueOf(zCopyFile && file4.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(n nVar, Boolean bool) {
        Context contextA;
        r.f(nVar, "this$0");
        r.e(bool, "it");
        if (!bool.booleanValue()) {
            m mVarI = nVar.i();
            if (mVarI != null) {
                m mVarI2 = nVar.i();
                String string = (mVarI2 == null || (contextA = mVarI2.a()) == null) ? null : contextA.getString(R.string.str_copy_video_failed);
                r.c(string);
                mVarI.f(string);
                return;
            }
            return;
        }
        m mVarI3 = nVar.i();
        if (mVarI3 != null) {
            mVarI3.f(HttpUrl.FRAGMENT_ENCODE_SET);
        }
        m mVarI4 = nVar.i();
        if (mVarI4 != null) {
            mVarI4.y(nVar.c);
        }
        Log.d("NewVideoPresenter", "mFinalVideoPath:" + nVar.c);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void u(n nVar, Throwable th) {
        r.f(nVar, "this$0");
        m mVarI = nVar.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.newvideo.h
                @Override // i.a.m.e
                public final boolean test(Object obj) {
                    return n.p(this.a, (String) obj);
                }
            }).k(new i.a.m.e() { // from class: com.seewo.factoryaging.newvideo.g
                @Override // i.a.m.e
                public final boolean test(Object obj) {
                    return n.q(this.a, (String) obj);
                }
            }).l(new i.a.m.d() { // from class: com.seewo.factoryaging.newvideo.e
                @Override // i.a.m.d
                public final Object a(Object obj) {
                    return n.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.newvideo.i
                @Override // i.a.m.c
                public final void accept(Object obj) {
                    n.t(this.a, (Boolean) obj);
                }
            }, new i.a.m.c() { // from class: com.seewo.factoryaging.newvideo.k
                @Override // i.a.m.c
                public final void accept(Object obj) {
                    n.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();
    }
}
