package com.ss.android.ugc.aweme.ecommerce.ordercenter;

import X.0CB;
import X.0CD;
import X.0CH;
import X.0CI;
import X.0CN;
import X.2HN;
import X.2Pr;
import X.46i;
import X.4Kv;
import X.4MY;
import X.4r1;
import X.6Is;
import X.ByO;
import X.C2G;
import X.C4717LoS;
import X.C4949LsC;
import X.C5043Lti;
import X.C5044Ltj;
import X.C5045Ltk;
import X.C5046Ltl;
import X.C5047Ltm;
import X.C5049Lto;
import X.C5050Ltp;
import X.C7O;
import X.C7R;
import X.CEE;
import X.CEF;
import X.DAe;
import X.DCr;
import X.M12;
import X.TSt;
import android.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.keva.Keva;
import com.bytedance.router.SmartRouter;
import com.google.gson.Gson;
import com.ss.android.ugc.aweme.IAccountUserService;
import com.ss.android.ugc.aweme.ecommerce.ordercenter.repository.api.OrderEntranceApi;
import com.ss.android.ugc.aweme.ecommerce.ordercenter.repository.dto.GetEntranceInfoResponse;
import com.ss.android.ugc.aweme.ecommerce.ordercenter.repository.dto.OrderEntranceType;
import com.ss.android.ugc.aweme.ecommerce.ordercenter.repository.dto.TradeEntranceInfo;
import java.util.Set;
import kotlin.h.b.n;

public final class OrderCenterEntry implements 6Is, 4Kv {
    public static final C5047Ltm LJFF = new C5047Ltm((byte) 0);
    public 4MY LIZ;
    public final Handler LIZIZ = new Handler(Looper.getMainLooper());
    public final Keva LIZJ = Keva.getRepo("ec_order_center_entrance_infos");
    public final Keva.OnChangeListener LIZLLL = new C5045Ltk(this);
    public boolean LJ;

    static {
        Covode.recordClassIndex(70955);
    }

    public final void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_DESTROY) {
            onDestory();
        }
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public final void onDestory() {
        this.LIZJ.unRegisterChangeListener(this.LIZLLL);
        this.LIZIZ.removeCallbacksAndMessages((Object) null);
        this.LIZ = null;
    }

    private final String LIZIZ() {
        IAccountUserService LJFF2 = 46i.LJFF();
        n.LIZIZ(LJFF2, "");
        String curUserId = LJFF2.getCurUserId();
        if (curUserId == null || curUserId.length() == 0) {
            return "unknown";
        }
        return curUserId;
    }

    private final TradeEntranceInfo LIZJ() {
        try {
            Object LIZ2 = new Gson().LIZ(this.LIZJ.getString(LIZIZ(), "{}"), TradeEntranceInfo.class);
            n.LIZIZ(LIZ2, "");
            return (TradeEntranceInfo) LIZ2;
        } catch (Throwable th) {
            TSt.LIZ(th);
            return new TradeEntranceInfo(0, (String) null, (String) null, (Integer) null, 15, (2HN) null);
        }
    }

    public final void LIZ() {
        boolean z;
        TradeEntranceInfo LIZJ2 = LIZJ();
        if (LIZJ2.getType() == OrderEntranceType.OPEN.ordinal()) {
            z = true;
        } else {
            z = false;
        }
        String tips = LIZJ2.getTips();
        if (tips == null) {
            tips = "";
        }
        Integer toReviewCount = LIZJ2.getToReviewCount();
        if (n.LIZ(Looper.getMainLooper(), Looper.myLooper())) {
            4MY r0 = this.LIZ;
            if (r0 != null) {
                r0.LIZ(z, tips);
            }
        } else {
            this.LIZIZ.post(new C5046Ltl(this, z, tips));
        }
        if (z && !this.LJ) {
            new C5050Ltp(toReviewCount).LIZLLL();
            this.LJ = true;
        }
    }

    private final void LIZ(C4949LsC<? super GetEntranceInfoResponse, 2Pr> lsC) {
        OrderEntranceApi.LIZ.LIZ().LIZIZ(new C4717LoS(lsC));
    }

    public final void LIZ(Activity activity) {
        M12.LIZ((Object) activity);
        TradeEntranceInfo LIZJ2 = LIZJ();
        if (activity instanceof 0CH) {
            if (ByO.LIZ()) {
                C7O.LIZIZ.LIZ();
                DAe.LIZ(0CI.LIZ((0CH) activity), DCr.LIZLLL, new CEE((4r1) null), 2);
            }
            if (C7R.LIZIZ()) {
                C7O.LIZIZ.LIZ();
                DAe.LIZ(0CI.LIZ((0CH) activity), DCr.LIZLLL, new CEF((4r1) null), 2);
            }
        }
        String schema = LIZJ2.getSchema();
        if (schema == null || schema.length() == 0) {
            LIZ((C4949LsC<? super GetEntranceInfoResponse, 2Pr>) new C5044Ltj(this, activity));
        } else {
            LIZ(activity, LIZJ2);
        }
    }

    public final void LIZ(TradeEntranceInfo tradeEntranceInfo) {
        TradeEntranceInfo tradeEntranceInfo2 = tradeEntranceInfo;
        Keva keva = this.LIZJ;
        n.LIZIZ(keva, "");
        String LIZIZ2 = LIZIZ();
        Gson gson = new Gson();
        if (tradeEntranceInfo2 == null) {
            tradeEntranceInfo2 = new TradeEntranceInfo(0, (String) null, (String) null, (Integer) null, 15, (2HN) null);
        }
        String LIZIZ3 = gson.LIZIZ(tradeEntranceInfo2);
        n.LIZIZ(LIZIZ3, "");
        C2G.LIZ(keva, LIZIZ2, LIZIZ3);
    }

    public final void LIZ(0CD r3, 4MY r4) {
        M12.LIZ((Object) r3, (Object) r4);
        if (this.LIZ == null) {
            this.LIZ = r4;
            r3.LIZ(this);
            this.LIZJ.registerChangeListener(this.LIZLLL);
            LIZ((C4949LsC<? super GetEntranceInfoResponse, 2Pr>) new C5043Lti(this));
            LIZ();
            return;
        }
        throw new RuntimeException("Do not init reduplicative!");
    }

    public final void LIZ(Activity activity, TradeEntranceInfo tradeEntranceInfo) {
        String str;
        String schema = tradeEntranceInfo.getSchema();
        if (schema == null || schema.length() == 0) {
            str = "aweme://ec/order_center";
        } else {
            str = tradeEntranceInfo.getSchema();
        }
        Uri parse = Uri.parse(str);
        Uri.Builder builder = new Uri.Builder();
        n.LIZIZ(parse, "");
        Uri.Builder encodedPath = builder.scheme(parse.getScheme()).encodedAuthority(parse.getAuthority()).encodedPath(parse.getPath());
        Set<String> queryParameterNames = parse.getQueryParameterNames();
        n.LIZIZ(queryParameterNames, "");
        for (String str2 : queryParameterNames) {
            encodedPath.appendQueryParameter(str2, parse.getQueryParameter(str2));
        }
        if (!parse.getQueryParameterNames().contains("previous_page")) {
            encodedPath.appendQueryParameter("previous_page", "Personal_Home_Page");
        }
        SmartRouter.buildRoute(activity, encodedPath.build().toString()).open();
        tradeEntranceInfo.setTips("");
        tradeEntranceInfo.setSchema("");
        LIZ(tradeEntranceInfo);
        new C5049Lto(tradeEntranceInfo.getToReviewCount()).LIZLLL();
    }
}
