package com.bokecc.livemodule.live.room;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

import com.kuaishou.akdanmaku.DanmakuConfig;
import com.kuaishou.akdanmaku.data.DanmakuItem;
import com.kuaishou.akdanmaku.ui.DanmakuDisplayer;
import com.kuaishou.akdanmaku.utils.Size;

import org.jetbrains.annotations.NotNull;


public class UpLogoRenderer extends CCLiveDanmakuRenderer {

    private final Drawable drawableSTu;
    private final Drawable drawablePub;
    private final Drawable drawableTea;
    private final Drawable drawableHost;

    private final Drawable drawableSTuBg;
    private final Drawable drawablePubBg;
    private final Drawable drawableTeaBg;
    private final Drawable drawableHostBg;

    private final Paint backgroundPaint;
    private final RectF rect;

    private static final int UP_BACKGROUND = Color.argb(102, 0, 0, 0);
    private static final int UP_SPACE_LOGO_TEXT = 8;
    private static final int UP_PADDING_VERTICAL = 12;
    private static final int UP_PADDING_HORIZONTAL = 26;
    private static final int CANVAS_PADDING = 6;


    public static final int ROLE_STUDENT = 0;
    public static final int ROLE_PUBLISHER = 1;
    public static final int ROLE_TEACHER = 2;
    public static final int ROLE_HOST = 3;

    public UpLogoRenderer(Context context, @Nullable Drawable drawableStudent, @NotNull Drawable drawablePublisher, @NotNull Drawable drawableTeacher, @NotNull Drawable drawableHost, @NotNull Drawable drawableStudentBg, @NotNull Drawable drawablePublisherBg, @NotNull Drawable drawableTeacherBg, @NotNull Drawable drawableHostBg) {
        super(context);
        this.drawableSTu = drawableStudent;
        this.drawablePub = drawablePublisher;
        this.drawableTea = drawableTeacher;
        this.drawableHost = drawableHost;

        this.drawableSTuBg = drawableStudentBg;
        this.drawablePubBg = drawablePublisherBg;
        this.drawableTeaBg = drawableTeacherBg;
        this.drawableHostBg = drawableHostBg;

        this.backgroundPaint = new Paint();
        this.rect = new RectF();

        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setColor(UP_BACKGROUND);
    }

    @Override
    public Size measure(@NotNull DanmakuItem item, @NotNull DanmakuDisplayer displayer, @NotNull DanmakuConfig config) {
        Size contentSize = super.measure(item, displayer, config);
        int logoWidth = getLogoSize(item, contentSize.getHeight());
        return new Size(
                contentSize.getWidth() + UP_PADDING_HORIZONTAL * 2 + logoWidth + UP_SPACE_LOGO_TEXT,
                contentSize.getHeight() + UP_PADDING_VERTICAL * 2
        );
    }

    @Override
    public void draw(@NotNull DanmakuItem item, @NotNull Canvas canvas, @NotNull DanmakuDisplayer displayer, @NotNull DanmakuConfig config) {

        int logoHeight = canvas.getHeight() - UP_PADDING_VERTICAL * 2;
        int logoWidth = getLogoSize(item,logoHeight);

        if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_PUBLISHER) {
            drawablePub.setBounds(
                    UP_PADDING_HORIZONTAL,
                    UP_PADDING_VERTICAL,
                    UP_PADDING_HORIZONTAL + logoWidth,
                    UP_PADDING_VERTICAL + logoHeight
            );
            drawablePubBg.setBounds(0,0,canvas.getWidth(), canvas.getHeight());
            drawablePubBg.draw(canvas);
            drawablePub.draw(canvas);
        } else if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_TEACHER) {
            drawableTea.setBounds(
                    UP_PADDING_HORIZONTAL,
                    UP_PADDING_VERTICAL,
                    UP_PADDING_HORIZONTAL + logoWidth,
                    UP_PADDING_VERTICAL + logoHeight
            );
            drawableTeaBg.setBounds(0,0,canvas.getWidth(), canvas.getHeight());
            drawableTeaBg.draw(canvas);
            drawableTea.draw(canvas);
        } else if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_HOST) {
            drawableHost.setBounds(
                    UP_PADDING_HORIZONTAL,
                    UP_PADDING_VERTICAL,
                    UP_PADDING_HORIZONTAL + logoWidth,
                    UP_PADDING_VERTICAL + logoHeight
            );
            drawableHostBg.setBounds(0,0,canvas.getWidth(), canvas.getHeight());
            drawableHostBg.draw(canvas);
            drawableHost.draw(canvas);
        } else {
            drawableSTuBg.setBounds(0,0,canvas.getWidth(), canvas.getHeight());
            drawableSTuBg.draw(canvas);
            if (drawableSTu!=null){
                drawableSTu.setBounds(
                        UP_PADDING_HORIZONTAL,
                        UP_PADDING_VERTICAL,
                        UP_PADDING_HORIZONTAL + logoWidth,
                        UP_PADDING_VERTICAL + logoHeight
                );
                drawableSTu.draw(canvas);
            }
        }

        canvas.translate(
                UP_PADDING_HORIZONTAL + logoWidth + UP_SPACE_LOGO_TEXT,
                UP_PADDING_VERTICAL
        );
        super.draw(item, canvas, displayer, config);
    }

    private int getLogoSize(DanmakuItem item, int contentHeight) {

        if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_PUBLISHER) {
            return (int) (contentHeight * (float) drawablePub.getIntrinsicWidth() / drawablePub.getIntrinsicHeight());

        } else if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_TEACHER) {
            return (int) (contentHeight * (float) drawableTea.getIntrinsicWidth() / drawableTea.getIntrinsicHeight());

        } else if (item.getData().getUserId() != null && item.getData().getUserId() == ROLE_HOST) {
            return (int) (contentHeight * (float) drawableHost.getIntrinsicWidth() / drawableHost.getIntrinsicHeight());
        } else {
            if (drawableSTu!=null){
                return (int) (contentHeight * (float) drawableSTu.getIntrinsicWidth() / drawableSTu.getIntrinsicHeight());
            }
            return 0;
        }
    }
}