package com.bc_chat.contacts.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bc_chat.circle.dto.EmojiconDatas;
import com.bc_chat.circle.entity.Emoji;
import com.bc_chat.contacts.R;
import com.zhaohaoting.framework.utils.Check;
import com.zhaohaoting.framework.view.indicator.IndicatorViewPager;

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

/**
 * author：Created by zhaohaoting on 2019/8/28
 * email：526309416@qq.com
 * desc：
 */
public class EmojiViewPagerAdapter extends IndicatorViewPager.IndicatorViewPagerAdapter {

    private static final int ROW = 3;
    private static final int COLUMN = 7;
    private final int pageTotal;
    private final Emoji[] emojis;

    public EmojiViewPagerAdapter() {
        emojis = EmojiconDatas.getData();
        pageTotal = ROW * COLUMN - 1;
    }

    @Override
    public int getCount() {
        return emojis.length % pageTotal > 0 ? emojis.length / pageTotal + 1 : emojis.length / pageTotal;
    }

    @Override
    public View getViewForTab(int position, View convertView, ViewGroup container) {
        if (convertView == null) {
            convertView = LayoutInflater.from(container.getContext()).inflate(R.layout.tab_indicator, container, false);
        }
        return convertView;
    }

    @Override
    public View getViewForPage(int position, View convertView, ViewGroup container) {
        Context context = container.getContext();
        RecyclerView recyclerView = new RecyclerView(context);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(context, COLUMN));
        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(getPagerEmojis(position));
        recyclerView.setAdapter(emojiGridAdapter);
        if (emojiClickListener != null) emojiGridAdapter.setEmojiClickListener(emojiClickListener);
        return recyclerView;
    }

    private List<Emoji> getPagerEmojis(int position) {
        List<Emoji> emojiList = new ArrayList<>();
        emojiList.addAll(Arrays.asList(this.emojis).subList(position * pageTotal, ((position + 1) * pageTotal) > emojis.length ? emojis.length : ((position + 1) * pageTotal)));
        emojiList.add(new Emoji(0, "", 2));
        return emojiList;
    }

    public static class EmojiGridAdapter extends RecyclerView.Adapter<EmojiHolder> {

        private List<Emoji> emojiData = new ArrayList<>();
        private EmojiClickListener emojiClickListener;

        public void setEmojiClickListener(EmojiClickListener emojiClickListener) {
            this.emojiClickListener = emojiClickListener;
        }

        public EmojiGridAdapter(List<Emoji> emojiData) {
            if (!Check.isEmpty(emojiData)) {
                this.emojiData.addAll(emojiData);
            }
        }

        @NonNull
        @Override
        public EmojiHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_emoji_layout, viewGroup, false);
            return new EmojiHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull EmojiHolder viewHolder, int i) {
            Emoji emoji = emojiData.get(i);
            viewHolder.updateItem(emoji);
            if (emojiClickListener != null)
                viewHolder.itemView.setOnClickListener(v -> {
                    if (emoji.getType() == 1) {
                        emojiClickListener.clickEmoji(emoji);
                    } else {
                        emojiClickListener.deleteEmoji();
                    }
                });
        }

        @Override
        public int getItemCount() {
            return emojiData.size();
        }
    }

    public static class EmojiHolder extends RecyclerView.ViewHolder {

        private final TextView textView;
        private final ImageView image;

        public EmojiHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            image = itemView.findViewById(R.id.image);
        }

        public void updateItem(Emoji emoji) {
            if (emoji.getType() == 1) {
                textView.setText(emoji.getEmojiText());
                image.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
            } else {
                image.setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
            }
        }
    }

    public void setEmojiClickListener(EmojiClickListener emojiClickListener) {
        this.emojiClickListener = emojiClickListener;
    }

    private EmojiClickListener emojiClickListener;

    public interface EmojiClickListener {
        void deleteEmoji();

        void clickEmoji(Emoji emoji);
    }
}
