package com.bokecc.livemodule.live.chat;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.bokecc.livemodule.R;
import com.bokecc.livemodule.live.chat.adapter.CustomEmojiAdapter;
import com.bokecc.livemodule.live.chat.adapter.DefaultEmojiAdapter;
import com.bokecc.livemodule.live.chat.util.DensityUtil;
import com.bokecc.livemodule.live.chat.util.EmojiItemDecoration;
import com.bokecc.livemodule.live.chat.util.EmojiUtil;
import com.bokecc.livemodule.view.CustomToast;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.CustomEmoji;

import java.util.ArrayList;
import java.util.Arrays;

public class EmojiLayout extends FrameLayout {
    private final Context mContext;
    private ImageView iv_default_emoji;
    private ImageView iv_custom_emoji;
    private RecyclerView gv_default_emoji;
    private RecyclerView gv_custom_emoji;
    private EmojiLayoutListener emojiLayoutListener;
    private CustomEmojiAdapter customEmojiAdapter;
    private int itemHeight = 0;

    public EmojiLayout(@NonNull Context context) {
        this(context, null);
    }

    public EmojiLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmojiLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_emoji_layout, this, true);
        iv_default_emoji = findViewById(R.id.iv_default_emoji);
        iv_custom_emoji = findViewById(R.id.iv_custom_emoji);
        gv_default_emoji = findViewById(R.id.gv_default_emoji);
        gv_custom_emoji = findViewById(R.id.gv_custom_emoji);
        findViewById(R.id.iv_del).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (emojiLayoutListener != null) {
                    emojiLayoutListener.delete();
                }
            }
        });
        iv_default_emoji.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTab(true);
            }
        });
        iv_custom_emoji.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DWLive.getInstance().isCustomEmojiDownloadComplete()) {
                    ArrayList<CustomEmoji> customEmojis = DWLive.getInstance().getCustomEmojis();
                    if (customEmojis.isEmpty()) {
                        CustomToast.showToast(getContext(), "自定义表情为空", Toast.LENGTH_SHORT);
                    } else {
                        initCustomEmoji(customEmojis);
                        changeTab(false);
                    }
                } else {
                    CustomToast.showToast(getContext(), "表情正在准备中", Toast.LENGTH_SHORT);
                }
            }
        });
        if (DWLive.getInstance().isOpenCustomEmoji()) {
            iv_custom_emoji.setVisibility(VISIBLE);
        } else {
            iv_custom_emoji.setVisibility(GONE);
        }
        itemHeight = DensityUtil.dp2px(mContext, 70);
        initDefaultEmoji();
    }

    private void initDefaultEmoji() {
        DefaultEmojiAdapter defaultEmojiAdapter = new DefaultEmojiAdapter(Arrays.copyOf(EmojiUtil.imgs, 20));
        defaultEmojiAdapter.setOnClickListener(new DefaultEmojiAdapter.OnClickListener() {
            @Override
            public void onItemClick(int position) {
                if (emojiLayoutListener != null) {
                    emojiLayoutListener.onItemClick(true, "", position);
                }
            }
        });
        gv_default_emoji.setLayoutManager(new GridLayoutManager(mContext, 7));
        gv_default_emoji.addItemDecoration(new EmojiItemDecoration(itemHeight));
        gv_default_emoji.setAdapter(defaultEmojiAdapter);
    }

    private void initCustomEmoji(ArrayList<CustomEmoji> customEmojis) {
        if (customEmojiAdapter == null) {
            customEmojiAdapter = new CustomEmojiAdapter(customEmojis);
            customEmojiAdapter.setOnClickListener(new CustomEmojiAdapter.OnClickListener() {
                @Override
                public void onItemClick(String name, int position) {
                    if (emojiLayoutListener != null) {
                        emojiLayoutListener.onItemClick(false, name, position);
                    }
                }
            });
            gv_custom_emoji.setLayoutManager(new GridLayoutManager(mContext, 7));
            gv_custom_emoji.addItemDecoration(new EmojiItemDecoration(itemHeight));
            gv_custom_emoji.setAdapter(customEmojiAdapter);
        }
    }

    public void reset() {
        changeTab(true);
    }

    private void changeTab(boolean isDefault) {
        if (isDefault) {
            iv_default_emoji.setBackgroundResource(R.drawable.shape_emoji_bg);
            iv_custom_emoji.setBackgroundResource(0);
            gv_default_emoji.setVisibility(VISIBLE);
            gv_custom_emoji.setVisibility(GONE);
        } else {
            iv_default_emoji.setBackgroundResource(0);
            iv_custom_emoji.setBackgroundResource(R.drawable.shape_emoji_bg);
            gv_default_emoji.setVisibility(GONE);
            gv_custom_emoji.setVisibility(VISIBLE);
        }
    }

    public void setEmojiLayoutListener(EmojiLayoutListener emojiLayoutListener) {
        this.emojiLayoutListener = emojiLayoutListener;
    }

    public interface EmojiLayoutListener {
        void onItemClick(boolean isDefault, String name, int position);

        void delete();
    }

}
