package com.bytedance.android.livesdk.like.widget;

import X.0CB;
import X.0CH;
import X.0Uo;
import X.0b0;
import X.0cm;
import X.6Is;
import X.C1059Fv2;
import X.C1064Fv7;
import X.C1430G2l;
import X.C2272GYv;
import X.C2402Gbb;
import X.C2821GiM;
import X.C3028Glh;
import X.C3491GtA;
import X.C3975H2i;
import X.C4100H7d;
import X.C4345HGo;
import X.C4453HKs;
import X.H3I;
import X.H3L;
import X.M12;
import X.Pap;
import X.TAL;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.ViewGroup;
import com.bytedance.android.live.base.model.ImageModel;
import com.bytedance.android.live.base.model.user.User;
import com.bytedance.android.live.liveinteract.api.IInteractService;
import com.bytedance.android.livesdk.IBarrageService;
import com.bytedance.android.livesdk.like.LikeHelper;
import com.bytedance.android.livesdk.like.OptimizedLikeHelper;
import com.bytedance.android.livesdk.livesetting.performance.degrade.LiveLikeDegradeSettings;
import com.bytedance.android.livesdk.model.UserAttr;
import com.bytedance.android.livesdk.model.message.LikeMessage;
import com.bytedance.android.livesdk.model.message.RoomMessage;
import com.bytedance.android.livesdkapi.depend.model.live.Room;
import com.bytedance.covode.number.Covode;
import com.bytedance.ies.sdk.datachannel.DataChannel;
import com.bytedance.ies.sdk.widgets.LiveRecyclableWidget;
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.LinkedList;
import java.util.List;
import kotlin.h.b.n;

public final class OthersLikeViewOptimizedWidget extends LiveRecyclableWidget implements Handler.Callback, H3L, 6Is, OnMessageListener {
    public static final H3I LIZ = new H3I((byte) 0);
    public TAL LIZIZ;
    public long LIZJ;
    public boolean LIZLLL;
    public IMessageManager LJ;
    public boolean LJFF;
    public LikeHelper LJI;
    public int LJII = 1;
    public long LJIIIIZZ = 300;
    public final Handler LJIIIZ = new Handler(Looper.getMainLooper(), this);
    public final LinkedList<Bitmap> LJIIJ = new LinkedList<>();
    public final LinkedList<Integer> LJIIJJI = new LinkedList<>();
    public int LJIIL;
    public int LJIILIIL;

    static {
        Covode.recordClassIndex(17402);
    }

    public final int getLayoutId() {
        return 2131561925;
    }

    public final void onStateChanged(0CH r1, 0CB r2) {
        super.onStateChanged(r1, r2);
    }

    private final void LIZIZ() {
        if (!this.LJIIJ.isEmpty()) {
            this.LJIIIZ.sendEmptyMessageDelayed(1, this.LJIIIIZZ);
        }
    }

    public final void onUnload() {
        TAL tal = this.LIZIZ;
        if (tal == null) {
            n.LIZ("");
        }
        tal.LIZ();
        IMessageManager iMessageManager = this.LJ;
        if (iMessageManager != null) {
            iMessageManager.removeMessageListener(this);
        }
        this.LJIIIZ.removeCallbacksAndMessages((Object) null);
        LIZ();
        this.LJIIL = 0;
        this.LJIILIIL = 0;
        this.LJIIJ.clear();
        this.LJIIJJI.clear();
        LikeHelper likeHelper = this.LJI;
        if (likeHelper != null && likeHelper.LJIILIIL) {
            likeHelper.LJJI = null;
        }
    }

    private final void LIZ() {
        Long l;
        Long l2;
        String str;
        0Uo LIZ2;
        UserAttr userAttr;
        if (this.LJIILIIL > 0) {
            C3491GtA LIZ3 = C3491GtA.LJFF.LIZ("livesdk_like_special_effect_show");
            LIZ3.LIZ(this.dataChannel);
            C3028Glh LIZIZ2 = C4100H7d.LIZ().LIZIZ();
            Room room = (Room) this.dataChannel.LIZIZ(C1064Fv7.class);
            Boolean bool = null;
            if (LIZIZ2 != null) {
                l = Long.valueOf(LIZIZ2.LIZJ());
            } else {
                l = null;
            }
            if (room != null) {
                l2 = Long.valueOf(room.getOwnerUserId());
            } else {
                l2 = null;
            }
            if (n.LIZ(l, l2)) {
                str = "anchor";
            } else {
                if (!(LIZIZ2 == null || (LIZ2 = LIZIZ2.LIZ()) == null || (userAttr = LIZ2.getUserAttr()) == null)) {
                    bool = Boolean.valueOf(userAttr.LIZIZ);
                }
                if (C1430G2l.LIZ(bool)) {
                    str = "admin";
                } else {
                    str = "user";
                }
            }
            LIZ3.LIZ("admin_type", str);
            LIZ3.LIZ("special_effect_amount", this.LJIILIIL);
            LIZ3.LIZ("action_type", C2402Gbb.LIZ.LJ());
            IInteractService LIZ4 = 0cm.LIZ(IInteractService.class);
            n.LIZIZ(LIZ4, "");
            if (n.LIZ(LIZ4.getConnectionType(), "manual_pk")) {
                IInteractService LIZ5 = 0cm.LIZ(IInteractService.class);
                n.LIZIZ(LIZ5, "");
                if (LIZ5.isBattling()) {
                    LIZ3.LIZ("match_status", "pk_phase");
                } else {
                    LIZ3.LIZ("match_status", "punish");
                }
            }
            LIZ3.LIZLLL();
            this.LJIILIIL = 0;
        }
    }

    public final void LIZ(Bitmap bitmap) {
        if (bitmap != null && !bitmap.isRecycled() && isViewValid()) {
            LIZ(2131235192, bitmap);
        }
    }

    public final void onInit(Object[] objArr) {
        TAL findViewById = findViewById(2131368290);
        n.LIZIZ(findViewById, "");
        this.LIZIZ = findViewById;
    }

    public final boolean handleMessage(Message message) {
        M12.LIZ(message);
        if (message.what == 1) {
            LIZ(this.LJIIJJI.poll(), this.LJIIJ.poll());
            LIZIZ();
            return true;
        } else if (message.what != 2) {
            return false;
        } else {
            LIZ();
            this.LJIIIZ.sendEmptyMessageDelayed(2, 60000);
            return false;
        }
    }

    public final void onMessage(IMessage iMessage) {
        Bitmap bitmap;
        if ((iMessage instanceof LikeMessage) && this.isViewValid) {
            if (this.LIZLLL && !this.LJFF) {
                this.LJFF = true;
                RoomMessage LIZ2 = C2821GiM.LIZ(this.LIZJ, this.context.getString(2131829143));
                IMessageManager iMessageManager = this.LJ;
                if (iMessageManager != null) {
                    iMessageManager.insertMessage(LIZ2, true);
                }
            }
            LikeMessage likeMessage = (LikeMessage) iMessage;
            User user = likeMessage.LIZLLL;
            if (user != null) {
                long id = user.getId();
                C3028Glh LIZIZ2 = C4100H7d.LIZ().LIZIZ();
                n.LIZIZ(LIZIZ2, "");
                if (id == LIZIZ2.LIZJ()) {
                    return;
                }
            }
            List<ImageModel> list = likeMessage.LJFF;
            LikeHelper likeHelper = this.LJI;
            if (likeHelper != null) {
                bitmap = likeHelper.LIZIZ(list);
            } else {
                bitmap = null;
            }
            LIZ(-1, bitmap);
        }
    }

    public final void onLoad(Object[] objArr) {
        long j;
        IMessageManager iMessageManager;
        int i;
        long j2;
        int LIZLLL2;
        Boolean bool;
        Room room;
        boolean z = false;
        this.LJFF = false;
        DataChannel dataChannel = this.dataChannel;
        if (dataChannel == null || (room = (Room) dataChannel.LIZIZ(C1064Fv7.class)) == null) {
            j = 0;
        } else {
            j = room.getId();
        }
        this.LIZJ = j;
        DataChannel dataChannel2 = this.dataChannel;
        if (!(dataChannel2 == null || (bool = (Boolean) dataChannel2.LIZIZ(C1059Fv2.class)) == null)) {
            z = bool.booleanValue();
        }
        this.LIZLLL = z;
        DataChannel dataChannel3 = this.dataChannel;
        LikeHelper likeHelper = null;
        if (dataChannel3 != null) {
            iMessageManager = (IMessageManager) dataChannel3.LIZIZ(C4345HGo.class);
        } else {
            iMessageManager = null;
        }
        this.LJ = iMessageManager;
        C2272GYv likeHelper2 = 0cm.LIZ(IBarrageService.class).getLikeHelper(this.LIZJ);
        if (likeHelper2 instanceof LikeHelper) {
            likeHelper = likeHelper2;
        }
        LikeHelper likeHelper3 = likeHelper;
        this.LJI = likeHelper3;
        if (likeHelper3 != null && likeHelper3.LJIILIIL && !LiveLikeDegradeSettings.INSTANCE.disableOtherLike(this.dataChannel)) {
            likeHelper3.LJJI = this;
            IMessageManager iMessageManager2 = this.LJ;
            if (iMessageManager2 != null) {
                iMessageManager2.addMessageListener(C4453HKs.LIKE.getIntType(), this);
            }
        }
        LikeHelper likeHelper4 = this.LJI;
        if (likeHelper4 != null) {
            i = likeHelper4.LJII;
        } else {
            i = 1;
        }
        this.LJII = i;
        LikeHelper likeHelper5 = this.LJI;
        if (likeHelper5 != null) {
            j2 = likeHelper5.LJIIIIZZ;
        } else {
            j2 = 300;
        }
        this.LJIIIIZZ = j2;
        this.LJIIIZ.sendEmptyMessageDelayed(2, 60000);
        TAL tal = this.LIZIZ;
        if (tal == null) {
            n.LIZ("");
        }
        ViewGroup.LayoutParams layoutParams = tal.getLayoutParams();
        LikeHelper likeHelper6 = this.LJI;
        if (likeHelper6 != null) {
            LIZLLL2 = likeHelper6.LJIJ();
        } else {
            LIZLLL2 = 0b0.LIZLLL(2131166032);
        }
        layoutParams.width = LIZLLL2;
        TAL tal2 = this.LIZIZ;
        if (tal2 == null) {
            n.LIZ("");
        }
        TAL tal3 = this.LIZIZ;
        if (tal3 == null) {
            n.LIZ("");
        }
        tal2.setLayoutParams(tal3.getLayoutParams());
    }

    private final void LIZ(int i, Bitmap bitmap) {
        if (i == -1) {
            int i2 = this.LJIIL + this.LJII;
            this.LJIIL = i2;
            C3975H2i.LIZ = i2;
        }
        int i3 = 0;
        if (this.LJIIIIZZ >= 0) {
            if (i == -1) {
                int i4 = this.LJII;
                while (i3 < i4) {
                    this.LJIIJ.offer(bitmap);
                    this.LJIIJJI.offer(Integer.valueOf(i));
                    i3++;
                }
            } else {
                this.LJIIJ.offer(bitmap);
                this.LJIIJJI.offer(Integer.valueOf(i));
            }
            if (!this.LJIIIZ.hasMessages(1)) {
                LIZIZ();
            }
        } else if (i == -1) {
            int i5 = this.LJII;
            while (i3 < i5) {
                LIZ(Integer.valueOf(i), bitmap);
                i3++;
            }
        } else {
            LIZ(Integer.valueOf(i), bitmap);
        }
    }

    private final void LIZ(Integer num, Bitmap bitmap) {
        int LIZLLL2;
        Bitmap bitmap2 = bitmap;
        if (bitmap2 != null) {
            if (num != null) {
                if (num.intValue() != -1) {
                    int intValue = num.intValue();
                    LikeHelper likeHelper = this.LJI;
                    if (likeHelper != null) {
                        likeHelper.LIZ(intValue);
                    }
                } else {
                    int i = this.LJIILIIL + 1;
                    this.LJIILIIL = i;
                    C3975H2i.LIZIZ = i;
                }
            }
            LikeHelper likeHelper2 = this.LJI;
            if (likeHelper2 != null) {
                LIZLLL2 = likeHelper2.LJIJ();
            } else {
                LIZLLL2 = 0b0.LIZLLL(2131166032);
            }
            int i2 = (LIZLLL2 - TAL.LIZLLL) / 2;
            int i3 = 0;
            if (i2 > 0) {
                i3 = Pap.Default.nextInt(-i2, i2);
            }
            TAL tal = this.LIZIZ;
            if (tal == null) {
                n.LIZ("");
            }
            float f = ((float) LIZLLL2) / 2.0f;
            float f2 = f + ((float) i3);
            tal.LIZ(bitmap2, f, OptimizedLikeHelper.LJJIIJZLJL, f2, OptimizedLikeHelper.LJJIIZ, f2, OptimizedLikeHelper.LJJIIJ);
        }
    }
}
