package com.gh.gamecenter.common.view.choosepic;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.RoundingParams;
import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.callback.OnListClickListener;
import com.gh.gamecenter.common.databinding.GameUploadPicItemBinding;
import com.gh.gamecenter.common.databinding.SuggestPicItemBinding;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.lightgame.utils.Utils;

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

/**
 * Created by khy on 2017/3/31.
 */
public class ChoosePicAdapter extends BaseRecyclerAdapter<ViewHolder> {

    private OnListClickListener mListListener;
    private OnItemDeleteListener mDeleteListener;
    private List<String> picList;
    private int maxSize = 5;
    private int mPicItem = R.layout.suggest_pic_item;
    private int mSuggestAddPicIcon = R.drawable.suggest_add_pic_icon;
    private int mScreenWidth = DisplayUtils.getScreenWidth();

    public ChoosePicAdapter(Context context, OnListClickListener listListener) {
        super(context);
        this.mListListener = listListener;
        picList = new ArrayList<>();
    }

    public ChoosePicAdapter(Context context, int maxSize, OnListClickListener listListener, OnItemDeleteListener deleteListener) {
        super(context);
        this.mListListener = listListener;
        this.mDeleteListener = deleteListener;
        picList = new ArrayList<>();
        this.maxSize = maxSize;
    }

    public void setPicItem(@LayoutRes int picItem) {
        this.mPicItem = picItem;
    }

    public void setSuggestAddPicIcon(@DrawableRes int suggestAddPicIcon) {
        this.mSuggestAddPicIcon = suggestAddPicIcon;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mPicItem == R.layout.game_upload_pic_item) {
            return new ChooseGameUploadPicViewHolder(GameUploadPicItemBinding.inflate(mLayoutInflater, parent, false), mListListener);
        } else {
            return new ChoosePicViewHolder(SuggestPicItemBinding.inflate(mLayoutInflater, parent, false), mListListener);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ChoosePicViewHolder) {
            ChoosePicViewHolder viewHolder = (ChoosePicViewHolder) holder;
            viewHolder.setClickData(picList);

            int spaceCount = maxSize - 1;
            int spaceWidth = DisplayUtils.dip2px(6F);
            int recyclerViewPadding = DisplayUtils.dip2px(26F);
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) viewHolder.binding.getRoot().getLayoutParams();
            layoutParams.setMargins(0, 0, position == spaceCount ? 0 : spaceWidth, 0);
            viewHolder.binding.getRoot().setLayoutParams(layoutParams);

            int itemWidth = (int) ((mScreenWidth - recyclerViewPadding - spaceWidth * spaceCount) / (float) maxSize);
            int imageViewWidth = itemWidth - DisplayUtils.dip2px(6F);
            ViewGroup.LayoutParams imageLayoutParams = viewHolder.binding.picItemIcon.getLayoutParams();
            imageLayoutParams.width = imageViewWidth;
            imageLayoutParams.height = imageViewWidth;
            viewHolder.binding.picItemIcon.setLayoutParams(imageLayoutParams);

            RoundingParams roundingParams = RoundingParams.fromCornersRadius(DisplayUtils.dip2px(8F));
            if (position == getItemCount() - 1 && picList.size() < maxSize) {
                viewHolder.binding.picItemDelate.setVisibility(View.GONE);
                viewHolder.binding.picItemIcon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
                ImageUtils.display(viewHolder.binding.picItemIcon, mSuggestAddPicIcon);
            } else {
                viewHolder.binding.picItemIcon.setImageURI("file:///" + picList.get(position));
                viewHolder.binding.picItemIcon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
                viewHolder.binding.picItemDelate.setImageResource(R.drawable.suggest_pic_delete);
                viewHolder.binding.picItemDelate.setVisibility(View.VISIBLE);
                roundingParams.setBorderColor(ContextCompat.getColor(mContext, R.color.black_alpha_10));
                roundingParams.setBorderWidth(DisplayUtils.dip2px(0.5F));
            }
            viewHolder.binding.picItemIcon.getHierarchy().setRoundingParams(roundingParams);

            viewHolder.binding.picItemDelate.setOnClickListener(v -> {
                if (mDeleteListener != null)
                    mDeleteListener.onItemDelete(picList.get(holder.getAdapterPosition()));
                picList.remove(holder.getAdapterPosition());
                notifyDataSetChanged();
            });
        } else if (holder instanceof ChooseGameUploadPicViewHolder) {
            ChooseGameUploadPicViewHolder viewHolder = (ChooseGameUploadPicViewHolder) holder;
            viewHolder.setClickData(picList);
            if (position == getItemCount() - 1 && picList.size() < maxSize) {
                viewHolder.binding.picItemDelate.setVisibility(View.GONE);
                ImageUtils.display(viewHolder.binding.picItemIcon, mSuggestAddPicIcon);
            } else {
                viewHolder.binding.picItemIcon.setImageURI("file:///" + picList.get(position));
                viewHolder.binding.picItemDelate.setVisibility(View.VISIBLE);
            }

            viewHolder.binding.picItemDelate.setOnClickListener(v -> {
                if (mDeleteListener != null)
                    mDeleteListener.onItemDelete(picList.get(holder.getAdapterPosition()));
                picList.remove(holder.getAdapterPosition());
                notifyDataSetChanged();
            });
        }
    }

    @Override
    public int getItemCount() {
//        return picList.size() + 1;
        return picList.size() < maxSize ? picList.size() + 1 : picList.size();
    }

    public void addFileList(String picPath) {
        for (String s : picList) {
            if (s.equals(picPath)) {
                Utils.toast(mContext, "图片已存在，无需重复添加");
                return;
            }
        }

        picList.add(picPath);
        notifyDataSetChanged();
    }

    public List<String> getFileList() {
        return picList;
    }

    public interface OnItemDeleteListener {
        void onItemDelete(String data);
    }
}
