package com.cooleshow.base.widgets;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.cooleshow.base.R;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.SoftKeyboardUtils;
import com.vanniktech.emoji.EmojiEditText;
import com.vanniktech.emoji.EmojiPopup;
import com.vanniktech.emoji.listeners.OnEmojiPopupDismissListener;
import com.vanniktech.emoji.listeners.OnEmojiPopupShownListener;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

/**
 * Created by gyn on 2021/11/12
 */
public class InputBar extends LinearLayout implements OnEmojiPopupShownListener, OnEmojiPopupDismissListener {
    private final static String TAG = InputBar.class.getSimpleName();
    private EmojiEditText etInput;
    private Space space1;
    private ImageView ivEmoji;
    private Space space2;
    private TextView tvSend;
    private InputBarListener inputBarListener;
    /**
     * emoji选择框
     */
    private EmojiPopup mEmojiPopup;

    public InputBar(Context context) {
        this(context, null);
    }

    public InputBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_inputbar, this);
        initView();
    }


    public void initView() {
        // init view
        this.setOrientation(HORIZONTAL);
        this.setBackgroundColor(getContext().getResources().getColor(R.color.white));
        this.setMinimumHeight(SizeUtils.dp2px(50));
        this.setGravity(Gravity.CENTER_VERTICAL);
        this.setPadding(SizeUtils.dp2px(12), SizeUtils.dp2px(7), SizeUtils.dp2px(12), SizeUtils.dp2px(7));
        etInput = (EmojiEditText) findViewById(R.id.et_input);
        space1 = (Space) findViewById(R.id.space1);
        ivEmoji = (ImageView) findViewById(R.id.iv_emoji);
        space2 = (Space) findViewById(R.id.space2);
        tvSend = (TextView) findViewById(R.id.tv_send);
        setEmoJiImage(R.drawable.ic_voice_room_emoji);
        setPopup();
        ivEmoji.setOnClickListener(v -> {
            if (inputBarListener != null) {
                boolean intercept = inputBarListener.onClickEmoji();
                if (intercept) {

                } else {
                    mEmojiPopup.toggle();
                }
            }
        });
        tvSend.setOnClickListener(v -> {
            send();
        });
        etInput.setOnEditorActionListener((v, actionId, event) -> {
            send();
            return false;
        });
    }

    private void setPopup() {
        mEmojiPopup = EmojiPopup
                .Builder
                .fromRootView(this)
                .setPopGravity(Gravity.LEFT)
                .setKeyboardAnimationStyle(R.style.my_emoji_fade_animation_style)
                .setOnEmojiPopupShownListener(this)
                .setOnEmojiPopupDismissListener(this).build(etInput);
    }

    private void setEmoJiImage(@DrawableRes int resId) {
        Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.black_333));
        ivEmoji.setImageDrawable(drawable);
    }

    public void send() {
        String message = "";
        if (etInput.getText() != null) {
            message = etInput.getText().toString().trim();
        }
        etInput.setText("");

        if (inputBarListener != null) {
            boolean isSuccess = inputBarListener.onClickSend(message);
            if (isSuccess) {
                etInput.setText("");
            }
        }
    }

    public void showInputBar() {
        setPopup();
//        this.setVisibility(VISIBLE);
        etInput.requestFocus();
        etInput.setFocusable(true);
        etInput.setFocusableInTouchMode(true);
        SoftKeyboardUtils.showSoftKeyboard(etInput, 50);
    }

    public void hideInputBar() {
        mEmojiPopup.dismiss();
//        this.setVisibility(GONE);
        etInput.clearFocus();
        SoftKeyboardUtils.hideSoftKeyboard(etInput);
    }

    public void setInputBarListener(InputBarListener inputBarListener) {
        this.inputBarListener = inputBarListener;
    }

    @Override
    public void onEmojiPopupDismiss() {
        setEmoJiImage(R.drawable.ic_voice_room_emoji);
    }

    @Override
    public void onEmojiPopupShown() {
        setEmoJiImage(R.drawable.ic_voice_room_keybroad);
    }

    public interface InputBarListener {

        boolean onClickSend(String message);

        boolean onClickEmoji();
    }
}
