package com.bytedance.android.livesdk.comp.impl.linkcore;

import X.0aP;
import X.0bV;
import X.0cm;
import X.2cN;
import X.7he;
import X.C0026TGa;
import X.C0027TGb;
import X.C0028TGc;
import X.C0029TGd;
import X.C0030TGe;
import X.C0031TGf;
import X.C0032TGg;
import X.C0033TGh;
import X.C0034TGi;
import X.C0035TGj;
import X.Crj;
import X.CwZ;
import X.Cwu;
import X.Glh;
import X.H7d;
import X.HKs;
import X.IWj;
import X.LWw;
import X.M12;
import X.Mjy;
import X.Mk3;
import X.PgU;
import X.TDf;
import X.TDw;
import X.TEq;
import X.TEu;
import X.TGT;
import X.TGV;
import X.TGX;
import X.TGY;
import X.TGZ;
import X.TH6;
import com.bytedance.android.live.base.model.user.User;
import com.bytedance.android.livesdk.comp.api.linkcore.ILinkCoreService;
import com.bytedance.android.livesdk.model.message.LinkMessage;
import com.bytedance.android.livesdk.model.message.MemberMessage;
import com.bytedance.android.livesdk.model.message.linkcore.LinkLayerMessage;
import com.bytedance.android.livesdkapi.depend.model.live.Room;
import com.bytedance.android.message.IMessageService;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.fresco.nativeheif.BuildConfig;
import com.ss.ugc.live.sdk.message.data.IMessage;
import com.ss.ugc.live.sdk.message.interfaces.IMessageManager;
import com.ss.ugc.live.sdk.message.interfaces.OnMessageListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import kotlin.h.b.n;

public class LinkCoreService implements ILinkCoreService, OnMessageListener {
    public static final C0032TGg Companion = new C0032TGg((byte) 0);
    public final Mjy mCompositeDisposable = new Mjy();
    public boolean mIsAnchor = true;
    public volatile boolean mIsInitialized;
    public final Crj mLinkEventListener$delegate = 7he.LIZ(new C0026TGa(this));
    public final CopyOnWriteArrayList<C0033TGh> mLinkerLifeCycleCallback = new CopyOnWriteArrayList<>();
    public final CopyOnWriteArrayList<C0035TGj> mLinkerList = new CopyOnWriteArrayList<>();
    public final Crj mLinkerListener$delegate = 7he.LIZ(new C0031TGf(this));
    public Room mRoom;
    public long mRoomId;
    public long mRoomOwnerUserId;
    public final HashMap<Integer, Boolean> sdkDisableMap;

    static {
        Covode.recordClassIndex(15018);
    }

    private final TGZ getMLinkEventListener() {
        return (TGZ) this.mLinkEventListener$delegate.getValue();
    }

    private final C0030TGe getMLinkerListener() {
        return (C0030TGe) this.mLinkerListener$delegate.getValue();
    }

    public void onInit() {
    }

    public List<C0035TGj> getLinkers() {
        return this.mLinkerList;
    }

    private final void clearAllLinkers() {
        TDw.LIZ.LIZ(new C0027TGb(this));
    }

    public boolean unbind() {
        if (!this.mIsInitialized) {
            return false;
        }
        this.mIsInitialized = false;
        0cm.LIZ(IMessageService.class).release(this.mRoomId);
        if (!this.mCompositeDisposable.isDisposed()) {
            this.mCompositeDisposable.dispose();
        }
        clearAllLinkers();
        return true;
    }

    public LinkCoreService() {
        0aP.LIZ(3, "link_layer_sdk_core_service", "init, this:".concat(String.valueOf(this)));
        this.sdkDisableMap = new HashMap<>(LWw.LIZ(new Cwu[]{CwZ.LIZ(4, false), CwZ.LIZ(2, false)}));
    }

    public void registerLinkerLiveCycleCallback(C0033TGh tGh) {
        if (tGh != null) {
            this.mLinkerLifeCycleCallback.add(tGh);
        }
    }

    public void removeLinkerLiveCycleCallback(C0033TGh tGh) {
        if (tGh != null) {
            this.mLinkerLifeCycleCallback.remove(tGh);
        }
    }

    public Boolean isDisableSDK(int i) {
        return this.sdkDisableMap.get(Integer.valueOf(i));
    }

    public final C0035TGj findTargetLinker(long j) {
        T t;
        Iterator<T> it = this.mLinkerList.iterator();
        while (true) {
            if (!it.hasNext()) {
                t = null;
                break;
            }
            t = it.next();
            if (((C0035TGj) t).LIZLLL() == j) {
                break;
            }
        }
        return (C0035TGj) t;
    }

    private final void onLinkLayerMessage(LinkLayerMessage linkLayerMessage) {
        TDw tDw = TDw.LIZ;
        2cN LIZ = PgU.LIZIZ(1).LIZ(Mk3.LIZ(IWj.LIZ.LIZ())).LIZ(new TGT(this, linkLayerMessage), TGX.LIZ);
        n.LIZIZ(LIZ, BuildConfig.VERSION_NAME);
        tDw.LIZ(LIZ, this.mCompositeDisposable);
    }

    private final void onLinkMessage(LinkMessage linkMessage) {
        TDw tDw = TDw.LIZ;
        2cN LIZ = PgU.LIZIZ(1).LIZ(Mk3.LIZ(IWj.LIZ.LIZ())).LIZ(new TGV(this, linkMessage), TGY.LIZ);
        n.LIZIZ(LIZ, BuildConfig.VERSION_NAME);
        tDw.LIZ(LIZ, this.mCompositeDisposable);
    }

    public boolean bindRoom(Room room) {
        M12.LIZ(room);
        boolean z = false;
        if (this.mIsInitialized) {
            return false;
        }
        this.mIsInitialized = true;
        this.mRoom = room;
        this.mRoomId = room.getId();
        this.mRoomOwnerUserId = room.getOwnerUserId();
        long ownerUserId = room.getOwnerUserId();
        Glh LIZIZ = H7d.LIZ().LIZIZ();
        n.LIZIZ(LIZIZ, BuildConfig.VERSION_NAME);
        if (ownerUserId == LIZIZ.LIZJ()) {
            z = true;
        }
        this.mIsAnchor = z;
        IMessageManager iMessageManager = 0cm.LIZ(IMessageService.class).get(this.mRoomId);
        if (iMessageManager != null) {
            iMessageManager.addMessageListener(HKs.BASE_LINK_LAYER_MESSAGE.getIntType(), this);
            iMessageManager.addMessageListener(HKs.MEMBER.getIntType(), this);
            iMessageManager.addMessageListener(HKs.LINK_MESSAGE.getIntType(), this);
        }
        return true;
    }

    public synchronized C0035TGj getLinker(int i) {
        MethodCollector.i(4117);
        if (i == 2) {
            TH6 th6 = new TH6(this.mRoom, i);
            th6.LIZ((C0034TGi) getMLinkerListener());
            0aP.LIZ(3, "link_layer_sdk_core_service", "MULTI_HOST get and add Linker ".concat(String.valueOf(th6)));
            this.mLinkerList.add(th6);
            MethodCollector.o(4117);
            return th6;
        } else if (i == 4) {
            TEu tEu = new TEu(this.mRoom, i);
            tEu.LIZ((TEq) getMLinkEventListener());
            0aP.LIZ(3, "link_layer_sdk_core_service", "MULTI_LIVE get and add Linker ".concat(String.valueOf(tEu)));
            this.mLinkerList.add(tEu);
            MethodCollector.o(4117);
            return tEu;
        } else {
            IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
            MethodCollector.o(4117);
            throw illegalArgumentException;
        }
    }

    public void onMessage(IMessage iMessage) {
        long j;
        if (iMessage instanceof LinkLayerMessage) {
            if (!n.LIZ(this.sdkDisableMap.get(4), true)) {
                onLinkLayerMessage((LinkLayerMessage) iMessage);
            } else {
                0aP.LIZ(6, "link_layer_sdk_core_service", "sdk is disabled, not to handle message");
            }
        } else if (iMessage instanceof LinkMessage) {
            LinkMessage linkMessage = (LinkMessage) iMessage;
            if (linkMessage.LIZJ == 2) {
                if (!n.LIZ(this.sdkDisableMap.get(2), true)) {
                    onLinkMessage(linkMessage);
                } else {
                    0aP.LIZ(6, "link_layer_sdk_core_service", "sdk is disabled, not to handle message");
                }
            }
        } else if (iMessage instanceof MemberMessage) {
            TDf tDf = TDf.LJI;
            User user = ((MemberMessage) iMessage).LIZIZ;
            if (user != null) {
                j = user.getId();
            } else {
                j = 0;
            }
            tDf.LIZ(j, true);
        }
    }

    public final void removeTargetLinker(C0035TGj tGj) {
        if (this.mLinkerList.contains(tGj)) {
            0aP.LIZ(3, "link_layer_sdk_core_service", "remove target linker ".concat(String.valueOf(tGj)));
            this.mLinkerList.remove(tGj);
            if (tGj.LJ() != 2) {
                tGj.LJIILJJIL();
            }
            for (C0033TGh tGh : this.mLinkerLifeCycleCallback) {
                0aP.LIZ(3, "link_layer_sdk_core_service", "onLinkerDestroyed:".concat(String.valueOf(tGh)));
                tGh.LIZIZ(tGj);
            }
        }
    }

    public final C0035TGj findAndCreateTargetLinker(IMessage iMessage) {
        if (iMessage instanceof LinkLayerMessage) {
            LinkLayerMessage linkLayerMessage = (LinkLayerMessage) iMessage;
            C0035TGj findTargetLinker = findTargetLinker(linkLayerMessage.LIZIZ);
            0aP.LIZ(3, "link_layer_sdk_core_service", "findAndCreateTargetLinker target linker ".concat(String.valueOf(findTargetLinker)));
            if (findTargetLinker != null) {
                return findTargetLinker;
            }
            TEu tEu = new TEu(linkLayerMessage.LIZIZ, this.mRoom, linkLayerMessage.LIZJ);
            tEu.LIZ((TEq) getMLinkEventListener());
            this.mLinkerList.add(tEu);
            0bV.LIZ(new C0029TGd(this, tEu));
            return tEu;
        } else if (iMessage instanceof LinkMessage) {
            StringBuilder sb = new StringBuilder("findAndCreateTargetLinker,scene:");
            LinkMessage linkMessage = (LinkMessage) iMessage;
            sb.append(linkMessage.LIZJ);
            sb.append(", message.channelId:");
            sb.append(linkMessage.LIZIZ);
            0aP.LIZ(3, "link_layer_sdk_core_service", sb.toString());
            C0035TGj findTargetLinker2 = findTargetLinker(linkMessage.LIZIZ);
            if (findTargetLinker2 != null) {
                return findTargetLinker2;
            }
            TH6 th6 = new TH6(this.mRoom, linkMessage.LIZJ);
            th6.LIZ((C0034TGi) getMLinkerListener());
            th6.LIZ(String.valueOf(linkMessage.LIZIZ));
            this.mLinkerList.add(th6);
            0bV.LIZ(new C0028TGc(th6, this, iMessage));
            return th6;
        } else {
            throw new IllegalArgumentException();
        }
    }

    public void setDisableSDK(int i, boolean z) {
        this.sdkDisableMap.put(Integer.valueOf(i), Boolean.valueOf(z));
    }
}
