package com.ironsource;

import android.app.Activity;
import com.ironsource.g2;
import com.ironsource.mediationsdk.IronSource;
import com.ironsource.mediationsdk.adunit.adapter.internal.AdapterAdFullScreenInterface;
import com.ironsource.mediationsdk.adunit.adapter.internal.BaseAdAdapter;
import com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener;
import com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdListener;
import com.ironsource.mediationsdk.logger.IronLog;
import com.ironsource.mediationsdk.logger.IronSourceError;
import com.ironsource.mediationsdk.model.Placement;
import com.ironsource.n7;
import java.util.Locale;
/* loaded from: classes4.dex */
public abstract class j7<Listener extends g2> extends n7<Listener> implements AdapterAdInteractionListener {

    /* loaded from: classes4.dex */
    class a extends cq {
        a() {
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.P();
        }
    }

    /* loaded from: classes4.dex */
    class b extends cq {
        b() {
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.S();
        }
    }

    /* loaded from: classes4.dex */
    class c extends cq {
        c() {
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.Q();
        }
    }

    /* loaded from: classes4.dex */
    class d extends cq {
        d() {
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.T();
        }
    }

    /* loaded from: classes4.dex */
    class e extends cq {
        e() {
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.R();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public class f extends cq {

        /* renamed from: a  reason: collision with root package name */
        final /* synthetic */ int f2462a;
        final /* synthetic */ String b;

        f(int i, String str) {
            this.f2462a = i;
            this.b = str;
        }

        @Override // com.ironsource.cq
        public void a() {
            j7.this.b(this.f2462a, this.b);
        }
    }

    public j7(po poVar, j1 j1Var, BaseAdAdapter<?, ?> baseAdAdapter, z2 z2Var, j5 j5Var, Listener listener) {
        super(poVar, j1Var, baseAdAdapter, z2Var, j5Var, listener);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void P() {
        String d2;
        IronLog ironLog = IronLog.INTERNAL;
        ironLog.verbose(d());
        synchronized (this.q) {
            if (this.e != n7.h.SHOWING) {
                ironLog.error("unexpected ad closed for " + k() + " - state = " + this.e);
                b2 b2Var = this.d;
                if (b2Var != null) {
                    b2Var.k.j("unexpected ad closed - state = " + this.e);
                }
                return;
            }
            a(n7.h.NONE);
            if (this.d != null) {
                String str = "";
                if (this.f2729a.a() == IronSource.AD_UNIT.REWARDED_VIDEO) {
                    str = "otherInstanceAvailable = " + (((g2) this.b).d().length() > 0 ? "true|" + d2 : "false");
                }
                this.d.j.a(j(), str);
            }
            ((g2) this.b).a(this);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void Q() {
        IronLog.INTERNAL.verbose(d());
        b2 b2Var = this.d;
        if (b2Var != null) {
            b2Var.j.d(j());
        }
        ((g2) this.b).c(this);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void R() {
        IronLog.INTERNAL.verbose(d());
        b2 b2Var = this.d;
        if (b2Var != null) {
            b2Var.j.l(j());
        }
        ((g2) this.b).b((j7<?>) this);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void S() {
        IronLog.INTERNAL.verbose(d());
        b2 b2Var = this.d;
        if (b2Var != null) {
            b2Var.j.i(j());
        }
        ((g2) this.b).d(this);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void T() {
        IronLog.INTERNAL.verbose(d());
        b2 b2Var = this.d;
        if (b2Var != null) {
            b2Var.j.k(j());
        }
    }

    static String a(n7.h hVar, int i, String str) {
        return String.format(Locale.ENGLISH, "unexpected show failed, state - %s, error - %d %s", hVar, Integer.valueOf(i), str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void b(int i, String str) {
        IronLog ironLog = IronLog.INTERNAL;
        ironLog.verbose(a("error = " + i + ", " + str));
        n7.h hVar = this.e;
        if (hVar == n7.h.SHOWING) {
            a(n7.h.FAILED);
            b2 b2Var = this.d;
            if (b2Var != null) {
                b2Var.j.a(j(), i, str, "");
            }
            ((g2) this.b).a(new IronSourceError(i, str), (j7<?>) this);
            return;
        }
        String a2 = a(hVar, i, str);
        ironLog.error(a(a2));
        b2 b2Var2 = this.d;
        if (b2Var2 != null) {
            b2Var2.k.r(a2);
        }
    }

    @Override // com.ironsource.n7
    public boolean B() {
        BaseAdAdapter<?, AdapterAdListener> baseAdAdapter;
        if (this.k == null || !y()) {
            return false;
        }
        try {
            baseAdAdapter = this.c;
        } catch (Throwable th) {
            l9.d().a(th);
            String str = "isReadyToShow - exception = " + th.getMessage() + " - state = " + this.e;
            IronLog.INTERNAL.error(a(str));
            b2 b2Var = this.d;
            if (b2Var != null) {
                b2Var.k.f(str);
            }
        }
        if (baseAdAdapter instanceof AdapterAdFullScreenInterface) {
            return ((AdapterAdFullScreenInterface) baseAdAdapter).isAdAvailable(this.k);
        }
        IronLog.INTERNAL.error(a("isReadyToShow - adapter not instance of AdapterAdFullScreenInterface"));
        b2 b2Var2 = this.d;
        if (b2Var2 != null) {
            b2Var2.k.f("isReadyToShow - adapter not instance of AdapterAdFullScreenInterface");
        }
        return false;
    }

    public void a(Activity activity, Placement placement) {
        IronLog ironLog = IronLog.INTERNAL;
        ironLog.verbose(a("placementName = " + placement.getPlacementName()));
        try {
            this.g = placement;
            a(n7.h.SHOWING);
            this.d.j.a(activity, j());
            BaseAdAdapter<?, AdapterAdListener> baseAdAdapter = this.c;
            if (baseAdAdapter instanceof AdapterAdFullScreenInterface) {
                ((AdapterAdFullScreenInterface) baseAdAdapter).showAd(this.k, this);
            } else {
                ironLog.error(a("showAd - adapter not instance of AdapterAdFullScreenInterface"));
                b2 b2Var = this.d;
                if (b2Var != null) {
                    b2Var.k.f("showAd - adapter not instance of AdapterAdFullScreenInterface");
                }
            }
        } catch (Throwable th) {
            l9.d().a(th);
            a(n7.h.FAILED);
            String str = "showAd - exception = " + th.getMessage() + " - state = " + this.e;
            IronLog.INTERNAL.error(a(str));
            b2 b2Var2 = this.d;
            if (b2Var2 != null) {
                b2Var2.k.f(str);
            }
            onAdShowFailed(x1.h(this.f2729a.a()), str);
        }
    }

    public void b(boolean z) {
        b2 b2Var = this.d;
        if (b2Var != null) {
            b2Var.j.a(z);
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdClosed() {
        if (u().c()) {
            u().a(new a());
        } else {
            P();
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdEnded() {
        if (u().c()) {
            u().a(new c());
        } else {
            Q();
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdShowFailed(int i, String str) {
        if (u().c()) {
            u().a(new f(i, str));
        } else {
            b(i, str);
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdShowSuccess() {
        if (u().c()) {
            u().a(new e());
        } else {
            R();
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdStarted() {
        if (u().c()) {
            u().a(new b());
        } else {
            S();
        }
    }

    @Override // com.ironsource.mediationsdk.adunit.adapter.internal.listener.AdapterAdInteractionListener
    public void onAdVisible() {
        if (u().c()) {
            u().a(new d());
        } else {
            T();
        }
    }
}
