package com.bykv.vk.openvk.preload.geckox.e;

import com.bykv.vk.openvk.preload.geckox.e.a.c;
import com.bykv.vk.openvk.preload.geckox.utils.g;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes.dex */
final class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private String f6092a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private String f6093b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private volatile File f6094c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private volatile Long f6095d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private volatile com.bykv.vk.openvk.preload.geckox.e.a.a f6096e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private AtomicBoolean f6097f = new AtomicBoolean(false);

    public a(String str, String str2) {
        this.f6092a = str;
        this.f6093b = str2;
    }

    private synchronized com.bykv.vk.openvk.preload.geckox.e.a.a d(String str) throws Exception {
        if (this.f6096e != null) {
            return this.f6096e;
        }
        File fileE = e(str);
        if (fileE == null) {
            throw new FileNotFoundException("channel no exist，channel:".concat(String.valueOf(str)));
        }
        File file = new File(fileE, "res.macv");
        File file2 = new File(fileE, "res");
        if (file2.exists() && file2.isDirectory()) {
            this.f6096e = new c(fileE);
        } else {
            if (!file.exists() || !file.isFile()) {
                throw new RuntimeException("can not find res, dir:" + fileE.getAbsolutePath());
            }
            this.f6096e = new com.bykv.vk.openvk.preload.geckox.e.a.b(fileE);
        }
        return this.f6096e;
    }

    private synchronized File e(String str) throws Exception {
        if (this.f6094c != null) {
            return this.f6094c;
        }
        if (this.f6095d != null && this.f6095d.longValue() == -1) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(this.f6092a);
        String str2 = File.separator;
        sb.append(str2);
        sb.append(str);
        sb.append(str2);
        sb.append("select.lock");
        com.bykv.vk.openvk.preload.geckox.f.b bVarA = com.bykv.vk.openvk.preload.geckox.f.b.a(sb.toString());
        try {
            if (this.f6095d == null) {
                this.f6095d = g.a(new File(this.f6092a, str));
            }
            if (this.f6095d == null) {
                this.f6095d = -1L;
                bVarA.a();
                return null;
            }
            File file = new File(this.f6092a, str2 + str + str2 + this.f6095d + str2 + "using.lock");
            this.f6094c = file.getParentFile();
            com.bykv.vk.openvk.preload.geckox.f.c.a(file.getAbsolutePath());
            File file2 = this.f6094c;
            bVarA.a();
            return file2;
        } catch (Throwable th) {
            bVarA.a();
            throw th;
        }
    }

    public final Long a() {
        return this.f6095d;
    }

    public final String b() {
        return this.f6093b;
    }

    public final int c(String str) {
        try {
            File file = new File(e(str), "res");
            if (!file.exists() || !file.isDirectory()) {
                return 0;
            }
            int length = file.listFiles().length;
            if (length > 0) {
                return length - 1;
            }
            return 0;
        } catch (Exception unused) {
            return 0;
        }
    }

    public final InputStream a(String str) throws Exception {
        return d(this.f6093b).a(a(this.f6093b, str));
    }

    public final boolean b(String str) throws Exception {
        return d(this.f6093b).b(a(this.f6093b, str));
    }

    private static String a(String str, String str2) {
        return str2.substring(str.length() + 1);
    }

    public final void c() throws Exception {
        if (this.f6097f.getAndSet(true)) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(this.f6092a);
        String str = File.separator;
        sb.append(str);
        sb.append(this.f6093b);
        sb.append(str);
        sb.append("select.lock");
        com.bykv.vk.openvk.preload.geckox.f.b bVarA = com.bykv.vk.openvk.preload.geckox.f.b.a(sb.toString());
        try {
            if (this.f6094c == null) {
                return;
            }
            com.bykv.vk.openvk.preload.geckox.f.c.b(this.f6094c.getAbsolutePath() + str + "using.lock");
            bVarA.a();
            com.bykv.vk.openvk.preload.geckox.a.c.a(this.f6092a + str + this.f6093b);
        } finally {
            bVarA.a();
        }
    }
}
