package com.ss.android.ugc.aweme.multi.maker;

import X.2UX;
import X.2nK;
import X.7gp;
import X.8eL;
import X.8qg;
import X.9EH;
import X.9HC;
import X.BA8;
import X.BAD;
import X.BAH;
import X.C0712ADk;
import X.C3192BAu;
import X.C4344Bi0;
import X.C4347Bi3;
import X.C4351Bi7;
import X.C4352Bi8;
import X.C4353Bi9;
import X.C4354BiA;
import X.C4355BiB;
import X.C4356BiC;
import X.C4357BiD;
import X.C4359BiF;
import X.C4360BiG;
import X.C4361BiH;
import X.IUt;
import X.JLw;
import X.Lel;
import X.LsH;
import X.M12;
import X.S9B;
import X.Sw0;
import X.Swe;
import android.app.Dialog;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.google.gson.Gson;
import com.google.gson.j;
import com.google.gson.m;
import com.google.gson.o;
import com.ss.android.ugc.aweme.ad.preload.AdLandPagePreloadServiceImpl;
import com.ss.android.ugc.aweme.feed.model.AnchorCommonStruct;
import com.ss.android.ugc.aweme.feed.model.Aweme;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import kotlin.h.b.n;
import kotlin.o.y;

public final class GamingAnchorMaker extends BA8 {
    public static final C4361BiH LIZLLL = new C4361BiH((byte) 0);
    public boolean LJ;
    public final C4351Bi7 LJFF = new C4351Bi7();
    public final AdjustAttributionUtil LJI = new AdjustAttributionUtil();
    public C4352Bi8 LJII;

    static {
        Covode.recordClassIndex(93267);
    }

    public final BAD LJIIIZ() {
        return new GamingAnchorMaker();
    }

    public final int LJIIJ() {
        return C3192BAu.TIKTOK_GAME.getTYPE();
    }

    public final boolean LJIILJJIL() {
        return LJIILL();
    }

    private final String LJJIII() {
        if (LJJIFFI()) {
            return "0";
        }
        if (LJJII()) {
            return "1";
        }
        return "2";
    }

    private final C4352Bi8 LJJ() {
        String extra;
        AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
        if (anchorCommonStruct == null || (extra = anchorCommonStruct.getExtra()) == null || extra.length() == 0) {
            return null;
        }
        if (this.LJII == null) {
            this.LJII = (C4352Bi8) new Gson().LIZ(extra, new C4355BiB().type);
        }
        return this.LJII;
    }

    private final boolean LJJIFFI() {
        BAH bah = BAH.LIZ;
        Collection anchors = LJIIZILJ().getAnchors();
        if (anchors == null) {
            anchors = Lel.INSTANCE;
        }
        if (bah.LIZ(new ArrayList(anchors)).size() == 1) {
            return true;
        }
        return false;
    }

    private final boolean LJJII() {
        BAH bah = BAH.LIZ;
        Collection anchors = LJIIZILJ().getAnchors();
        if (anchors == null) {
            anchors = Lel.INSTANCE;
        }
        ArrayList<AnchorCommonStruct> LIZ = bah.LIZ(new ArrayList(anchors));
        if (LIZ.size() > 0 && LIZ.get(0).getType() == C3192BAu.TIKTOK_GAME.getTYPE()) {
            return true;
        }
        return false;
    }

    private final String LJIILLIIL() {
        C4352Bi8 LJJ = LJJ();
        if (LJJ != null) {
            int i = LJJ.LJ;
            if (i != 1) {
                if (i != 2) {
                    AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
                    if (anchorCommonStruct != null) {
                        return anchorCommonStruct.getSchema();
                    }
                    return null;
                } else if (7gp.LIZ(9EH.LJJIFFI.LIZ(), LJJ.LJFF)) {
                    return LJJ.LJI.LIZ;
                } else {
                    AnchorCommonStruct anchorCommonStruct2 = this.LJIIIIZZ;
                    if (anchorCommonStruct2 != null) {
                        return anchorCommonStruct2.getSchema();
                    }
                    return null;
                }
            } else if (7gp.LIZ(9EH.LJJIFFI.LIZ(), LJJ.LJFF)) {
                return LJJ.LJI.LIZ;
            } else {
                return Uri.parse("aweme://google_play?package_name=" + LJJ.LJFF).buildUpon().appendQueryParameter("to_page", "store").build().toString();
            }
        } else {
            AnchorCommonStruct anchorCommonStruct3 = this.LJIIIIZZ;
            if (anchorCommonStruct3 != null) {
                return anchorCommonStruct3.getSchema();
            }
            return null;
        }
    }

    private final void LJJI() {
        String str;
        m mVar;
        if (LJIILL()) {
            o oVar = new o();
            AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
            j jVar = null;
            if (anchorCommonStruct != null) {
                str = anchorCommonStruct.getExtra();
            } else {
                str = null;
            }
            j LIZ = oVar.LIZ(str);
            if (LIZ != null) {
                mVar = LIZ.LJIIL();
            } else {
                mVar = null;
            }
            Gson gson = new Gson();
            if (mVar != null) {
                jVar = mVar.LIZJ("gecko_channel");
            }
            AdLandPagePreloadServiceImpl.LJFF().LIZ().LIZJ((List) gson.LIZ(jVar, new C4354BiA().type));
        }
    }

    public final String LJIILIIL() {
        String str;
        String str2;
        m mVar = new m();
        try {
            mVar.LIZ("enter_from", LJIJ());
            mVar.LIZ("from_source", "item_anchor");
            mVar.LIZ("from_group_id", LJIIZILJ().getAid());
            mVar.LIZ("from_author_id", LJIIZILJ().getAuthorUid());
            AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
            if (anchorCommonStruct != null) {
                str = anchorCommonStruct.getId();
            } else {
                str = null;
            }
            mVar.LIZ("scene_id", str);
            if (LJJIFFI()) {
                str2 = "item_play";
            } else {
                str2 = "anchor_list";
            }
            mVar.LIZ("position", str2);
            mVar.LIZ("anchor_type", LJJIII());
        } catch (Exception unused) {
        }
        String jVar = mVar.toString();
        n.LIZIZ(jVar, "");
        return jVar;
    }

    public final boolean LJIILL() {
        String str;
        String str2;
        m mVar;
        AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
        j jVar = null;
        if (anchorCommonStruct != null) {
            str = anchorCommonStruct.getExtra();
        } else {
            str = null;
        }
        if (str == null || str.length() == 0) {
            return false;
        }
        o oVar = new o();
        AnchorCommonStruct anchorCommonStruct2 = this.LJIIIIZZ;
        if (anchorCommonStruct2 != null) {
            str2 = anchorCommonStruct2.getExtra();
        } else {
            str2 = null;
        }
        j LIZ = oVar.LIZ(str2);
        if (LIZ != null) {
            mVar = LIZ.LJIIL();
        } else {
            mVar = null;
        }
        Gson gson = new Gson();
        if (mVar != null) {
            jVar = mVar.LIZJ("is_schema_lynx");
        }
        Boolean bool = (Boolean) gson.LIZ(jVar, new C4347Bi3().type);
        if (bool != null) {
            return bool.booleanValue();
        }
        return false;
    }

    public static final class AdjustAttributionUtil {
        public static final 9HC LIZLLL = new 9HC((byte) 0);
        public boolean LIZ;
        public final Handler LIZIZ = new Handler(Looper.getMainLooper());
        public Runnable LIZJ;
        public boolean LJ;

        public interface AdjustApi {
            static {
                Covode.recordClassIndex(93269);
            }

            @IUt
            JLw<String> monitor(@8qg String str);
        }

        static {
            Covode.recordClassIndex(93268);
        }

        public final void LIZIZ(String str) {
            if (!this.LJ) {
                this.LJ = true;
                LIZ(str);
            }
        }

        public final void LIZ(String str) {
            if (str != null && str.length() != 0) {
                8eL.LIZ().submit(new C4353Bi9(str));
            }
        }
    }

    public final void LIZIZ(2UX r2) {
        M12.LIZ(r2);
        this.LJ = true;
        LJJI();
        super.LIZIZ(r2);
    }

    private final String LIZ(String str) {
        String queryParameter;
        String LJIILLIIL = LJIILLIIL();
        if (LJIILLIIL == null || (queryParameter = Uri.parse(LJIILLIIL).getQueryParameter(str)) == null) {
            return "";
        }
        return queryParameter;
    }

    public final void LIZJ(2UX r4) {
        M12.LIZ(r4);
        super.LIZJ(r4);
        if (!LJJIFFI()) {
            LIZ("mp_click", LJJIII(), "item_play");
        }
    }

    public final void LIZLLL(2UX r6) {
        String str;
        String str2;
        C4359BiF biF;
        M12.LIZ(r6);
        if (!this.LJ) {
            LJJI();
        }
        C4344Bi0 bi0 = new C4344Bi0(this, r6);
        String LJIILLIIL = LJIILLIIL();
        if (LJIILLIIL == null || !y.LIZIZ(LJIILLIIL, "aweme://google_play?package_name=", false)) {
            bi0.invoke(LJIILLIIL);
        } else {
            LsH.LIZ(LsH.LIZ(), LJIILLIIL);
            AdjustAttributionUtil adjustAttributionUtil = this.LJI;
            9HC r3 = AdjustAttributionUtil.LIZLLL;
            C4352Bi8 LJJ = LJJ();
            if (LJJ == null || (biF = LJJ.LJII) == null) {
                str2 = null;
            } else {
                str2 = biF.LIZIZ;
            }
            String LIZ = r3.LIZ(str2, LJIJ(), "item_anchor");
            if (!adjustAttributionUtil.LIZ) {
                adjustAttributionUtil.LIZ = true;
                adjustAttributionUtil.LIZ(LIZ);
            }
        }
        String LJJIII = LJJIII();
        if (LJJIFFI()) {
            str = "item_play";
        } else {
            str = "anchor_list";
        }
        LIZ("mp_click", LJJIII, str);
    }

    public final void LIZ(Aweme aweme) {
        super.LIZ(aweme);
        AdjustAttributionUtil adjustAttributionUtil = this.LJI;
        Runnable runnable = adjustAttributionUtil.LIZJ;
        if (runnable != null) {
            adjustAttributionUtil.LIZIZ.removeCallbacks(runnable);
        }
        adjustAttributionUtil.LIZJ = null;
    }

    public final void LIZ(C0712ADk aDk, Sw0 sw0, Swe swe) {
        String str;
        C4359BiF biF;
        M12.LIZ(aDk, sw0, swe);
        C4351Bi7 bi7 = this.LJFF;
        bi7.LIZ = new HashMap<>();
        bi7.LIZIZ = new HashMap<>();
        LIZ("mp_show", LJJIII(), "item_play");
        if (!this.LJ) {
            LJJI();
        }
        if (LJJIFFI()) {
            AdjustAttributionUtil adjustAttributionUtil = this.LJI;
            9HC r3 = AdjustAttributionUtil.LIZLLL;
            C4352Bi8 LJJ = LJJ();
            if (LJJ == null || (biF = LJJ.LJII) == null) {
                str = null;
            } else {
                str = biF.LIZ;
            }
            String LIZ = r3.LIZ(str, LJIJ(), "item_anchor");
            if (adjustAttributionUtil.LIZJ == null) {
                adjustAttributionUtil.LIZJ = new C4360BiG(adjustAttributionUtil, LIZ);
                Runnable runnable = adjustAttributionUtil.LIZJ;
                if (runnable != null) {
                    adjustAttributionUtil.LIZIZ.postDelayed(runnable, 3000);
                }
            }
        }
    }

    private final void LIZ(String str, String str2, String str3) {
        String extra;
        String str4;
        m mVar;
        j jVar;
        String str5;
        AnchorCommonStruct anchorCommonStruct = this.LJIIIIZZ;
        j jVar2 = null;
        if (anchorCommonStruct != null && (extra = anchorCommonStruct.getExtra()) != null && extra.length() != 0 && this.LJFF.LIZ(str, str3)) {
            o oVar = new o();
            AnchorCommonStruct anchorCommonStruct2 = this.LJIIIIZZ;
            if (anchorCommonStruct2 != null) {
                str4 = anchorCommonStruct2.getExtra();
            } else {
                str4 = null;
            }
            j LIZ = oVar.LIZ(str4);
            if (LIZ != null) {
                mVar = LIZ.LJIIL();
            } else {
                mVar = null;
            }
            2UX r2 = new 2UX();
            Gson gson = new Gson();
            if (mVar != null) {
                jVar = mVar.LIZJ("game_id");
            } else {
                jVar = null;
            }
            r2.LIZ("game_id", (String) gson.LIZ(jVar, new C4356BiC().type));
            Gson gson2 = new Gson();
            if (mVar != null) {
                jVar2 = mVar.LIZJ("game_name");
            }
            r2.LIZ("game_name", (String) gson2.LIZ(jVar2, new C4357BiD().type));
            C4352Bi8 LJJ = LJJ();
            String str6 = "";
            if (LJJ == null || (str5 = LJJ.LJIIIIZZ) == null) {
                str5 = str6;
            }
            r2.LIZ("global_game_id", str5);
            r2.LIZ("to_page", LIZ("to_page"));
            r2.LIZ("to_page_id", LIZ("to_page_id"));
            r2.LIZ("enter_from", LJIJ());
            String aid = LJIIZILJ().getAid();
            if (aid == null) {
                aid = str6;
            }
            r2.LIZ("group_id", aid);
            String authorUid = LJIIZILJ().getAuthorUid();
            if (authorUid != null) {
                str6 = authorUid;
            }
            r2.LIZ("author_id", str6);
            r2.LIZ("anchor_id", LIZIZ().LIZ());
            r2.LIZ("anchor_type", str2);
            r2.LIZ("position", str3);
            r2.LIZ("params_for_special", "game_platform");
            r2.LIZ("region", S9B.LIZ());
            2nK.LIZ(str, r2.LIZ);
        }
    }

    public final void LIZ(C0712ADk aDk, Dialog dialog, boolean z, boolean z2) {
        String str;
        C4359BiF biF;
        M12.LIZ(aDk);
        super.LIZ(aDk, dialog, z, z2);
        LIZ("mp_show", LJJIII(), "anchor_list");
        AdjustAttributionUtil adjustAttributionUtil = this.LJI;
        9HC r3 = AdjustAttributionUtil.LIZLLL;
        C4352Bi8 LJJ = LJJ();
        if (LJJ == null || (biF = LJJ.LJII) == null) {
            str = null;
        } else {
            str = biF.LIZ;
        }
        adjustAttributionUtil.LIZIZ(r3.LIZ(str, LJIJ(), "item_anchor"));
    }
}
