package com.flurry.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import com.tapjoy.TapjoyConstants;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/0.dex */
public final class aj extends o implements DialogInterface.OnKeyListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, ai {
    AdUnit e;
    int f;
    bb g;
    private final String h;
    private ProgressDialog i;
    private at j;
    private WebView k;
    private AlertDialog l;
    private List<AdFrame> m;
    private boolean n;
    private Map<String, AdUnit> o;
    private Map<String, bb> p;
    private Context q;
    private be r;
    private w s;
    private boolean t;
    private String u;
    private List<String> v;

    /* JADX INFO: Access modifiers changed from: package-private */
    public aj(Context context, be beVar, bb bbVar, AdUnit adUnit, int i) {
        super(context, beVar, bbVar);
        this.h = getClass().getSimpleName();
        this.t = false;
        this.u = null;
        this.q = context;
        this.e = adUnit;
        this.f = i;
        this.g = bbVar;
        if (this.e != null) {
            this.m = this.e.c();
            this.n = this.e.d().intValue() == 1;
            if (this.n) {
                this.p = new HashMap();
                this.o = new HashMap();
                this.p.put(bbVar.b(), bbVar);
                this.o.put(adUnit.c().get(0).f().toString(), adUnit);
            }
            this.r = beVar;
            this.s = this.r.c;
            this.c = this.e;
            this.b = this.g;
            this.v = new LinkedList();
        }
    }

    private void f() {
        ViewParent parent;
        if (e().equals(AdCreative.kFormatTakeover)) {
            try {
                ((Activity) this.q).finish();
                a("adClosed", Collections.emptyMap(), this.e, this.g, this.f, 0);
                return;
            } catch (ClassCastException e) {
                String str = "caught class cast exception: " + e;
                return;
            }
        }
        ViewParent parent2 = getParent();
        if (parent2 != null && (parent = parent2.getParent()) != null) {
            try {
                be beVar = this.r;
                Context context = this.q;
                this.e.a().toString();
                beVar.a(context, (ViewGroup) parent);
            } catch (ClassCastException e2) {
                String str2 = "failed to remove view from holder: " + e2.getMessage();
            }
        }
    }

    @Override // com.flurry.android.o
    public final void initLayout(Context context) {
        removeAllViews();
        int c = c();
        try {
            Activity activity = (Activity) this.q;
            if (c == 3) {
                activity.setRequestedOrientation(0);
            }
        } catch (ClassCastException e) {
            e.toString();
        }
        setFocusable(true);
        setFocusableInTouchMode(true);
        switch (c()) {
            case 1:
            case 2:
                if (this.k == null) {
                    this.k = new WebView(context);
                    this.k.setWebViewClient(new aq(this));
                    this.k.getSettings().setJavaScriptEnabled(true);
                    this.k.setVerticalScrollBarEnabled(false);
                    this.k.setHorizontalScrollBarEnabled(false);
                    this.k.setBackgroundColor(0);
                }
                if (c() == 1) {
                    if (this.u != null) {
                        this.k.loadUrl(this.u);
                    } else {
                        this.k.loadUrl(g());
                    }
                } else if (c() == 2) {
                    this.k.loadDataWithBaseURL("base://url/", g(), "text/html", "utf-8", "base://url/");
                }
                this.k.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
                return;
            case 3:
                if (this.j == null) {
                    this.j = new at(context);
                    this.j.setOnPreparedListener(this);
                    this.j.setOnCompletionListener(this);
                    this.j.setOnErrorListener(this);
                    this.j.setMediaController(new MediaController(context));
                }
                this.j.setVideoURI(Uri.parse(g()));
                bc.a(this.h, "URI: " + Uri.parse(g()).toString());
                this.j.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
                addView(this.j);
                ((RelativeLayout) this.j.getParent()).setGravity(17);
                this.i = new ProgressDialog(context);
                this.i.setProgressStyle(0);
                this.i.setMessage("Loading...");
                this.i.setCancelable(true);
                this.i.setOnKeyListener(this);
                this.i.show();
                return;
            default:
                a("renderFailed", Collections.emptyMap(), this.e, this.g, this.f, 0);
                return;
        }
    }

    @Override // com.flurry.android.ai
    public final void a(u uVar, be beVar, int i) {
        int i2;
        int i3;
        int i4;
        String str;
        String str2 = uVar.a;
        ba baVar = uVar.c;
        Map<String, String> map = uVar.b;
        if (i > 10) {
            r0 = "Maximum depth for event/action loop exceeded when performing action:" + str2 + "," + map + ",triggered by:" + baVar.a;
            return;
        }
        String str3 = "performAction(action=" + str2 + ",params=" + uVar.b + ",triggering event=" + baVar.a + ")";
        if (str2.equals("nextAdUnit")) {
            try {
                t tVar = (t) getParent();
                if (tVar != null) {
                    if (!uVar.b.containsKey("delay")) {
                        i2 = 30;
                    } else {
                        try {
                            i2 = Integer.parseInt(uVar.b.get("delay"));
                        } catch (NumberFormatException e) {
                            i2 = 30;
                        }
                    }
                    tVar.a(i2 * 1000);
                    this.a.d.a(tVar);
                }
            } catch (ClassCastException e2) {
                e2.toString();
                f();
            }
        } else if (str2.equals("nextFrame")) {
            int i5 = this.f + 1;
            String str4 = map.get("offset");
            if (str4 != null) {
                if (str4.equals("next")) {
                    i5 = this.f + 1;
                } else if (!str4.equals("current")) {
                    try {
                        i5 = Integer.parseInt(str4);
                    } catch (NumberFormatException e3) {
                        str = "caught: " + e3.getMessage();
                    }
                } else {
                    return;
                }
            }
            if (this.n) {
                String str5 = uVar.c.b.get(TapjoyConstants.TJC_GUID);
                if (str5 != null) {
                    this.e = b(str5);
                    this.m = this.e.c();
                    this.g = uVar.c.e;
                    if (e().equals(AdCreative.kFormatTakeover)) {
                        this.r.a(this.e);
                        this.r.b(this.g);
                    }
                    this.f = i5;
                    this.n = false;
                    initLayout(this.q);
                }
            } else if (i5 != this.f && i5 < this.m.size()) {
                String e4 = e();
                String obj = this.e.c().get(i5).d().d().toString();
                if (obj.equals(e4)) {
                    this.f = i5;
                    initLayout(this.q);
                } else if (obj.equals(AdCreative.kFormatTakeover)) {
                    this.r.b(this.g);
                    this.r.a(this.e);
                    Intent intent = new Intent();
                    intent.putExtra("frameIndex", i5);
                    intent.setClass(this.q, FlurryFullscreenTakeoverActivity.class);
                    this.q.startActivity(intent);
                }
            }
        } else if (str2.equals("closeAd")) {
            f();
        } else if (str2.equals("notifyUser")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this.q);
            builder.setMessage(map.containsKey("message") ? map.get("message") : "Are you sure?").setCancelable(false).setPositiveButton(map.containsKey("confirmDisplay") ? map.get("confirmDisplay") : "Yes", new ao(this, baVar, i)).setNegativeButton(map.containsKey("cancelDisplay") ? map.get("cancelDisplay") : "No", new al(this, baVar, i));
            this.l = builder.create();
            if (this.j != null && c() == 3) {
                this.j.pause();
            }
            this.l.show();
        } else if (str2.equals("loadAdComponents")) {
            int i6 = 1;
            int i7 = 3;
            if (map.containsKey("min") && map.containsKey("max")) {
                try {
                    i6 = Integer.parseInt(map.get("min"));
                    i7 = Integer.parseInt(map.get("max"));
                } catch (NumberFormatException e5) {
                    i6 = 1;
                    i7 = 3;
                }
            }
            this.e.a().toString();
            List<AdUnit> a = a(i6, i7);
            if (a.size() > 0) {
                this.e.a().toString();
                this.k.loadUrl("javascript:(function() {var multiadwraps=document.getElementsByClassName('multiAdWrap');if(multiadwraps.length>0){var template=document.getElementsByClassName('multiAdWrap')[0];var compiled=Hogan.compile(template.innerHTML);template.innerHTML='';template.innerHTML=compiled.render(JSON.parse(" + a(a) + "));}})();");
                this.k.loadUrl("javascript:flurryadapter.callComplete();");
                for (AdUnit adUnit : a) {
                    HashMap hashMap = new HashMap();
                    hashMap.put(TapjoyConstants.TJC_GUID, adUnit.c().get(0).f().toString());
                    a("rendered", hashMap, adUnit, a(adUnit.c().get(0).f().toString()), 0, 0);
                }
                if (!a(this.k)) {
                    addView(this.k);
                }
            } else {
                a("renderFailed", Collections.emptyMap(), this.e, this.g, this.f, 0);
            }
        } else if (str2.equals("doExpand")) {
            int a2 = y.a(this.q);
            int b = y.b(this.q);
            if (uVar.c.b.containsKey(AdCreative.kFixWidth) && uVar.c.b.containsKey(AdCreative.kFixHeight)) {
                try {
                    i3 = Integer.parseInt(uVar.c.b.get(AdCreative.kFixWidth));
                    i4 = Integer.parseInt(uVar.c.b.get(AdCreative.kFixHeight));
                } catch (NumberFormatException e6) {
                    e6.getMessage();
                    i3 = y.a(this.q);
                    i4 = y.b(this.q);
                }
            } else {
                i3 = a2;
                i4 = b;
            }
            try {
                t tVar2 = (t) getParent();
                if (tVar2 != null) {
                    tVar2.a(i3, i4);
                }
                if (uVar.c.b.containsKey("url")) {
                    this.u = uVar.c.b.get("url");
                    initLayout(this.q);
                }
                this.t = true;
            } catch (ClassCastException e7) {
                e7.getMessage();
            }
        } else if (str2.equals("doCollapse")) {
            int intValue = this.m.get(this.f).d().a().intValue();
            int intValue2 = this.m.get(this.f).d().b().intValue();
            int b2 = y.b(this.q, intValue);
            int b3 = y.b(this.q, intValue2);
            if (this.u != null) {
                this.u = null;
                initLayout(this.q);
            }
            try {
                ((t) getParent()).a(b2, b3);
            } catch (ClassCastException e8) {
                r0 = "action doCollapse failed:" + e8.getMessage();
            }
            this.t = false;
        } else {
            this.a.a(uVar, beVar, i);
        }
        if (!this.v.contains(uVar.c.a) || this.k == null) {
            return;
        }
        this.k.loadUrl("javascript:flurryadapter.callComplete('" + uVar.c.a + "');");
        this.v.remove(uVar.c.a);
    }

    @Override // android.media.MediaPlayer.OnCompletionListener
    public final void onCompletion(MediaPlayer mediaPlayer) {
        a("videoCompleted", Collections.emptyMap(), this.e, this.g, this.f, 0);
    }

    @Override // android.media.MediaPlayer.OnPreparedListener
    public final void onPrepared(MediaPlayer mediaPlayer) {
        if (c() == 3) {
            if (this.i.isShowing()) {
                this.i.dismiss();
            }
            if (this.l == null || !this.l.isShowing()) {
                this.j.start();
            }
            a("rendered", Collections.emptyMap(), this.e, this.g, this.f, 0);
            a("videoStart", Collections.emptyMap(), this.e, this.g, this.f, 0);
        }
    }

    @Override // android.media.MediaPlayer.OnErrorListener
    public final boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
        if (this.i != null && this.i.isShowing()) {
            this.i.dismiss();
        }
        a("renderFailed", Collections.emptyMap(), this.e, this.g, this.f, 0);
        removeView(this.j);
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void b() {
        if (c() == 3) {
            if (this.i != null && this.i.isShowing()) {
                this.i.dismiss();
            }
            if (this.j != null && this.j.isPlaying()) {
                this.j.stopPlayback();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(String str, Map<String, String> map, AdUnit adUnit, bb bbVar, int i, int i2) {
        String str2 = "fireEvent(event=" + str + ",params=" + map + ")";
        this.a.a(new ba(str, map, this.q, adUnit, bbVar, i), this, i2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final int c() {
        return this.m.get(this.f).a().intValue();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final String d() {
        return this.m.get(this.f).c().toString();
    }

    private String g() {
        return this.m.get(this.f).b().toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final String e() {
        return this.m.get(this.f).d().d().toString();
    }

    private static String a(List<AdUnit> list) {
        StringBuilder sb = new StringBuilder();
        sb.append("'{\"adComponents\":[");
        Iterator<AdUnit> it = list.iterator();
        while (it.hasNext()) {
            sb.append(y.e(it.next().c().get(0).c().toString()));
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        sb.append("]}'");
        return sb.toString();
    }

    private List<AdUnit> a(int i, int i2) {
        List<AdUnit> a = this.s.a(this.e.a().toString(), i2);
        for (AdUnit adUnit : a) {
            if (adUnit.c().size() > 0) {
                this.o.put(adUnit.c().get(0).f().toString(), adUnit);
            }
        }
        return a;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public bb a(String str) {
        if (this.p == null) {
            return null;
        }
        bb bbVar = this.p.get(str);
        if (bbVar == null) {
            bb a = y.a(this.r, str);
            this.p.put(str, a);
            return a;
        }
        return bbVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public AdUnit b(String str) {
        if (this.o == null) {
            return null;
        }
        return this.o.get(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.flurry.android.o
    public final boolean a() {
        return !this.t;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final boolean a(View view) {
        ViewParent parent = view.getParent();
        return parent != null && parent == this;
    }

    @Override // android.content.DialogInterface.OnKeyListener
    public final boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
        String str = "onkey,keycode=" + i + ",event=" + keyEvent.getAction();
        if (dialogInterface == this.i && i == 4 && keyEvent.getAction() == 0) {
            a("adWillClose", Collections.emptyMap(), this.e, this.g, this.f, 0);
            dialogInterface.dismiss();
            return true;
        }
        return false;
    }
}
