package com.bytedance.sdk.component.adexpress.fm.ro;

import android.text.TextUtils;
import android.util.Pair;
import com.bytedance.sdk.component.adexpress.fm.lb.fm;
import com.bytedance.sdk.component.utils.ol;
import com.google.android.gms.measurement.internal.u1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class lb {
    public abstract File fm();

    public boolean fm(Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> map) {
        if (map == null || map.size() == 0) {
            return false;
        }
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar = map.get(it.next());
            if (fmVar != null && !fm(fmVar.ajl())) {
                return false;
            }
        }
        return true;
    }

    public void lb(List<fm.C0032fm> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        Iterator<fm.C0032fm> it = list.iterator();
        while (it.hasNext()) {
            File file = new File(fm(), com.bytedance.sdk.component.utils.jnr.fm(it.next().fm()));
            File file2 = new File(file + ".tmp");
            if (file.exists()) {
                try {
                    file.delete();
                } catch (Throwable unused) {
                }
            }
            if (file2.exists()) {
                try {
                    file2.delete();
                } catch (Throwable unused2) {
                }
            }
        }
    }

    public List<fm.C0032fm> ro(com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar2) {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        List<fm.C0032fm> arrayList3 = new ArrayList<>();
        if (fmVar2 == null || fmVar2.ajl().isEmpty()) {
            arrayList2.addAll(fmVar.ajl());
        } else if (fmVar.ajl().isEmpty()) {
            arrayList.addAll(fmVar2.ajl());
        } else {
            for (fm.C0032fm c0032fm : fmVar.ajl()) {
                if (!fmVar2.ajl().contains(c0032fm) && c0032fm != null && c0032fm.fm() != null && c0032fm.ro() != null) {
                    arrayList2.add(c0032fm);
                }
            }
            for (fm.C0032fm c0032fm2 : fmVar2.ajl()) {
                if (!fmVar.ajl().contains(c0032fm2)) {
                    arrayList.add(c0032fm2);
                }
            }
        }
        if (fm(arrayList2, arrayList3)) {
            return arrayList;
        }
        return null;
    }

    public boolean fm(List<fm.C0032fm> list) {
        if (list == null || list.size() <= 0 || fm() == null) {
            return false;
        }
        for (fm.C0032fm c0032fm : list) {
            String strFm = com.bytedance.sdk.component.utils.jnr.fm(c0032fm.fm());
            if (TextUtils.isEmpty(strFm)) {
                return false;
            }
            File file = new File(fm(), strFm);
            String strFm2 = com.bytedance.sdk.component.utils.jnr.fm(file);
            if (!file.exists() || !file.isFile() || c0032fm.ro() == null || !c0032fm.ro().equals(strFm2)) {
                return false;
            }
        }
        return true;
    }

    public static boolean lb(com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar2) {
        if (fmVar != null) {
            try {
                if (!TextUtils.isEmpty(fmVar.lb())) {
                    if (fmVar2 == null) {
                        return false;
                    }
                    String strWsy = fmVar.wsy();
                    String strWsy2 = fmVar2.wsy();
                    if ((!TextUtils.isEmpty(strWsy2) && !strWsy2.equals(strWsy)) || fm(fmVar.lb(), fmVar2.lb())) {
                        return true;
                    }
                    Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> mapFm = fmVar.fm();
                    Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> mapFm2 = fmVar2.fm();
                    if (mapFm.isEmpty()) {
                        return !mapFm2.isEmpty();
                    }
                    if (mapFm2.isEmpty()) {
                        return false;
                    }
                    return fm(mapFm, mapFm2);
                }
            } catch (Throwable th) {
                th.getMessage();
                return false;
            }
        }
        return true;
    }

    public boolean fm(fm.ro roVar) {
        if (roVar == null || fm() == null) {
            return false;
        }
        List<Pair<String, String>> listRo = roVar.ro();
        if (listRo == null || listRo.size() <= 0) {
            return true;
        }
        Iterator<Pair<String, String>> it = listRo.iterator();
        while (it.hasNext()) {
            File file = new File(fm(), (String) it.next().first);
            if (!file.exists() || !file.isFile()) {
                return false;
            }
        }
        return true;
    }

    public void ro(List<fm.C0032fm> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        Iterator<fm.C0032fm> it = list.iterator();
        while (it.hasNext()) {
            File file = new File(fm(), com.bytedance.sdk.component.utils.jnr.fm(it.next().fm()));
            File file2 = new File(file + ".tmp");
            if (file.exists()) {
                try {
                    file.delete();
                } catch (Throwable unused) {
                }
            }
            if (file2.exists()) {
                try {
                    file2.delete();
                } catch (Throwable unused2) {
                }
            }
        }
    }

    public List<fm.C0032fm> fm(com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar2) {
        Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> mapFm = fmVar.fm();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        List<fm.C0032fm> arrayList3 = new ArrayList<>();
        if (mapFm.size() == 0) {
            if (fmVar2 != null && fmVar2.fm().size() != 0) {
                Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> mapFm2 = fmVar2.fm();
                Iterator<String> it = mapFm2.keySet().iterator();
                while (it.hasNext()) {
                    com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar3 = mapFm2.get(it.next());
                    if (fmVar3 != null) {
                        arrayList.addAll(fmVar3.ajl());
                    }
                }
            }
        } else if (fmVar2 != null && fmVar2.fm().size() != 0) {
            Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> mapFm3 = fmVar2.fm();
            for (String str : mapFm.keySet()) {
                com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar4 = mapFm.get(str);
                com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar5 = mapFm3.get(str);
                if (fmVar5 == null && fmVar4 != null) {
                    arrayList2.addAll(fmVar4.ajl());
                } else if (fmVar4 == null && fmVar5 != null) {
                    arrayList.addAll(fmVar5.ajl());
                } else if (fmVar4 != null) {
                    for (fm.C0032fm c0032fm : fmVar4.ajl()) {
                        if (c0032fm != null && !fmVar5.ajl().contains(c0032fm) && c0032fm.ro() != null && c0032fm.fm() != null) {
                            arrayList2.add(c0032fm);
                        }
                    }
                    for (fm.C0032fm c0032fm2 : fmVar5.ajl()) {
                        if (c0032fm2 != null && !fmVar4.ajl().contains(c0032fm2)) {
                            arrayList.add(c0032fm2);
                        }
                    }
                }
            }
        } else if (mapFm.size() != 0) {
            Iterator<String> it2 = mapFm.keySet().iterator();
            while (it2.hasNext()) {
                com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar6 = mapFm.get(it2.next());
                if (fmVar6 != null) {
                    arrayList2.addAll(fmVar6.ajl());
                }
            }
        }
        if (fm(arrayList2, arrayList3)) {
            return arrayList;
        }
        return null;
    }

    public static void ro(File file, com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, String str) {
        if (fmVar == null || file == null) {
            return;
        }
        try {
            new File(file, str).delete();
        } catch (Throwable unused) {
        }
        if (fmVar.ajl() != null) {
            Iterator<fm.C0032fm> it = fmVar.ajl().iterator();
            while (it.hasNext()) {
                try {
                    new File(file, com.bytedance.sdk.component.utils.jnr.fm(it.next().fm())).delete();
                } catch (Throwable unused2) {
                }
            }
        }
    }

    private boolean fm(List<fm.C0032fm> list, List<fm.C0032fm> list2) {
        for (fm.C0032fm c0032fm : list) {
            String strFm = c0032fm.fm();
            String strFm2 = com.bytedance.sdk.component.utils.jnr.fm(strFm);
            File file = new File(fm(), strFm2);
            File file2 = new File(file + ".tmp");
            if (file.exists()) {
                try {
                    file.delete();
                } catch (Throwable unused) {
                }
            }
            if (file2.exists()) {
                try {
                    file2.delete();
                } catch (Throwable unused2) {
                }
            }
            com.bytedance.sdk.component.wsy.ro.fm fmVarAjl = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().lb().ajl();
            fmVarAjl.lb(strFm);
            fmVarAjl.fm(fm().getAbsolutePath(), strFm2);
            com.bytedance.sdk.component.wsy.ro roVarJnr = fmVarAjl.jnr();
            list2.add(c0032fm);
            if (roVarJnr == null || !roVarJnr.ajl() || roVarJnr.jnr() == null || !roVarJnr.jnr().exists()) {
                lb(list2);
                return false;
            }
        }
        return true;
    }

    public boolean fm(String str) {
        File file = new File(fm().getAbsoluteFile(), u1.j(com.bytedance.sdk.component.utils.jnr.fm(str), ".zip"));
        com.bytedance.sdk.component.wsy.ro.fm fmVarAjl = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().lb().ajl();
        fmVarAjl.lb(str);
        fmVarAjl.fm(file.getParent(), file.getName());
        com.bytedance.sdk.component.wsy.ro roVarJnr = fmVarAjl.jnr();
        if (roVarJnr.ajl() && roVarJnr.jnr() != null && roVarJnr.jnr().exists()) {
            File fileJnr = roVarJnr.jnr();
            try {
                ol.fm(fileJnr.getAbsolutePath(), file.getParent());
                if (!fileJnr.exists()) {
                    return true;
                }
                fileJnr.delete();
                return true;
            } catch (Exception unused) {
            }
        }
        return false;
    }

    public void fm(int i10) {
        if (com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().yz() != null) {
            com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().yz().fm(i10);
        }
    }

    public static void fm(File file, com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, String str) {
        FileOutputStream fileOutputStream;
        if (fmVar == null) {
            return;
        }
        String strVt = fmVar.vt();
        if (TextUtils.isEmpty(strVt)) {
            return;
        }
        File file2 = new File(file, str);
        File file3 = new File(file2 + ".tmp");
        if (file3.exists()) {
            file3.delete();
        }
        try {
            fileOutputStream = new FileOutputStream(file3);
        } catch (Throwable unused) {
            fileOutputStream = null;
        }
        try {
            fileOutputStream.write(strVt.getBytes("utf-8"));
            if (file2.exists()) {
                file2.delete();
            }
            file3.renameTo(file2);
            try {
                fileOutputStream.close();
            } catch (IOException unused2) {
            }
        } catch (Throwable unused3) {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException unused4) {
                }
            }
        }
    }

    private static boolean fm(Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> map, Map<String, com.bytedance.sdk.component.adexpress.fm.lb.fm> map2) {
        if (map.size() != map2.size()) {
            return true;
        }
        for (String str : map2.keySet()) {
            com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar = map.get(str);
            if (fmVar == null) {
                return true;
            }
            com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar2 = map2.get(str);
            if (fmVar2 == null) {
                return false;
            }
            if (fm(fmVar.lb(), fmVar2.lb())) {
                return true;
            }
        }
        return false;
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0042, code lost:
    
        return false;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean fm(String str, String str2) {
        String[] strArrSplit = str2.split("\\.");
        String[] strArrSplit2 = str.split("\\.");
        int iMin = Math.min(strArrSplit.length, strArrSplit2.length);
        int i10 = 0;
        while (true) {
            if (i10 >= iMin) {
                break;
            }
            int length = strArrSplit[i10].length() - strArrSplit2[i10].length();
            if (length == 0) {
                int iCompareTo = strArrSplit[i10].compareTo(strArrSplit2[i10]);
                if (iCompareTo > 0) {
                    return true;
                }
                if (iCompareTo < 0) {
                    return false;
                }
                if (i10 == iMin - 1) {
                    return strArrSplit.length > strArrSplit2.length;
                }
                i10++;
            } else if (length > 0) {
                return true;
            }
        }
    }

    public static boolean fm(com.bytedance.sdk.component.adexpress.fm.lb.fm fmVar, String str) {
        if (fmVar == null) {
            return true;
        }
        try {
            if (TextUtils.isEmpty(fmVar.lb())) {
                return true;
            }
            if (TextUtils.isEmpty(str)) {
                return false;
            }
            return fm(fmVar.lb(), str);
        } catch (Throwable unused) {
            return false;
        }
    }
}
