package com.xunao.effectdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.xunao.effectdemo.R;

import java.util.List;

public class WordCutAdapter extends RecyclerView.Adapter<WordCutAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_FOOT = 1;
    public static final int TYPE_BODY = 2;
    private List<String> wordList;
    private final Context context;

    public WordCutAdapter(List<String> mList, Context context) {
        this.wordList = mList;
        this.context = context;
    }

    @NonNull
    @Override
    public WordCutAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_word_cut,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);
        if(i == TYPE_HEADER){
            holder.llLeftArea.setVisibility(View.VISIBLE);
            holder.llSlideArea.setVisibility(View.VISIBLE);
        }else if(i == TYPE_FOOT){
            holder.llRightArea.setVisibility(View.VISIBLE);
        }else{
            holder.llSlideArea.setVisibility(View.VISIBLE);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull WordCutAdapter.ViewHolder viewHolder, int i) {
        String data = wordList.get(i);
        viewHolder.tvWord.setText(data);

        int[] location = new int[2];
        viewHolder.llSlideArea.getLocationInWindow(location);
        Log.i("word", i + "---x:" + location[0] + ",y:" + location[1]);

        viewHolder.llItem.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_MOVE:
                        Log.i("word", "X:" + event.getRawX() + ",Y:" + event.getRawY());
                        break;
                    case MotionEvent.ACTION_UP:
                        viewHolder.llDivider.setVisibility(View.VISIBLE);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return TYPE_HEADER;
        }else if(position == wordList.size() -1){
            return TYPE_FOOT;
        }else{
            return TYPE_BODY;
        }
    }

    @Override
    public int getItemCount() {
        return wordList.isEmpty() ? 0 : wordList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvWord;
        public LinearLayout llSlideArea;
        LinearLayout llLeftArea;
        LinearLayout llRightArea;
        LinearLayout llDivider;
        LinearLayout llItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvWord = itemView.findViewById(R.id.tv_word);
            llSlideArea = itemView.findViewById(R.id.ll_slide_area);
            llLeftArea = itemView.findViewById(R.id.ll_left_area);
            llRightArea = itemView.findViewById(R.id.ll_right_area);
            llDivider = itemView.findViewById(R.id.ll_divider);
            llItem = itemView.findViewById(R.id.ll_item);
        }
    }

    //1.定义变量接收接口
    private OnItemClickListener mOnItemClickListener;
    //2.定义接口：点击事件
    public interface OnItemClickListener {
        void onItemClick(View view, int position);//单击
    }
    //3.设置接口接收的方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

}
