package com.yidianling.dynamic.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.miracle.view.imageeditor.utils.FileUtils;
import com.ydl.ydl_image.module.GlideApp;
import com.ydl.ydl_image.transform.GlideRoundTransform;
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.model.RecommendTrendImage;

import java.util.List;

/**
 * Created by xiongyu on 2017/3/17.
 */

public class PublishTrendImgAdapter extends RecyclerView.Adapter<PublishTrendImgViewHolder> {
    private static final String TAG = "RecommendTrendImgAdapte";

    protected Context mContext;
    protected List<RecommendTrendImage> mDatas;
    protected LayoutInflater mInflater;

    public interface OnItemClickLister {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);

        void onDeleteLongClick(View view, int position);
    }

    public interface OnAddClickLister {
        void OnAddClick(View view, int position);

        void OnAddLongClick(View view, int position);
    }

    private OnItemClickLister mOnItemClickLister;
    private OnDeleteClickLister mOnDeleteClickLister;
    private OnAddClickLister mOnAddClickLister;

    public void setOnItemClickLister(OnItemClickLister lister) {
        this.mOnItemClickLister = lister;
    }

    public void setOnDeleteClickLister(OnDeleteClickLister lister) {
        this.mOnDeleteClickLister = lister;
    }

    public void setOnAddClickLister(OnAddClickLister lister) {
        this.mOnAddClickLister = lister;
    }

    public PublishTrendImgAdapter(List<RecommendTrendImage> mDatas, Context mContext) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public PublishTrendImgViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        view = mInflater.inflate(R.layout.dynamic_item_publish_trend_img, parent, false);
        PublishTrendImgViewHolder viewHolder = new PublishTrendImgViewHolder(view);
        return viewHolder;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(PublishTrendImgViewHolder holder, final int position) {
        RecommendTrendImage trendImage = mDatas.get(position);
        String path = trendImage.getImage_url();

        if ("default".equals(path)) {
            holder.bg_iv.setVisibility(View.VISIBLE);
            holder.item_publish_iv.setVisibility(View.GONE);
            holder.item_publish_cancel_iv.setVisibility(View.GONE);
        } else {
            holder.item_publish_cancel_iv.setVisibility(View.VISIBLE);
            holder.bg_iv.setVisibility(View.GONE);
            holder.item_publish_iv.setVisibility(View.VISIBLE);
            Uri uriByPath = FileUtils.INSTANCE.getUriByPath(mContext, path);
            GlideApp.with(mContext)
                    .load(uriByPath)
                    .error(R.drawable.dynamic_default_img)
                    .transform(new GlideRoundTransform(mContext, 6))
//                    .centerCrop()
                    .into(holder.item_publish_iv);
        }

        setUpItemEvent(holder);
        setUpDeleteEvent(holder);
        setUpAddEvent(holder);
    }

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


    public void updateDate(List<RecommendTrendImage> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    /**
     * 添加据到指定位置
     *
     * @param pos        数据添加的位置
     * @param trendImage 数据
     */
    public void addData(int pos, RecommendTrendImage trendImage) {
        mDatas.add(trendImage);
        notifyItemInserted(pos);
    }

    /**
     * 删除指定位置数据
     *
     * @param pos
     */
    public void deleteData(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }

    /**
     * 给item添加监听
     *
     * @param holder
     */
    protected void setUpItemEvent(final PublishTrendImgViewHolder holder) {
        if (mOnItemClickLister != null) {
            holder.item_publish_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickLister.onItemClick(holder.item_publish_iv, layoutPosition);

                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickLister.onItemLongClick(holder.item_publish_iv, layoutPosition);
                    return false;
                }
            });
        }
    }

    /**
     * 添加监听
     *
     * @param holder
     */
    protected void setUpDeleteEvent(final PublishTrendImgViewHolder holder) {
        if (mOnItemClickLister != null) {
            holder.item_publish_cancel_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnDeleteClickLister.onDeleteClick(holder.item_publish_cancel_iv, layoutPosition);

                }
            });
            holder.item_publish_cancel_iv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnDeleteClickLister.onDeleteLongClick(holder.item_publish_cancel_iv, layoutPosition);
                    return false;
                }
            });
        }
    }

    /**
     * 添加监听
     *
     * @param holder
     */
    protected void setUpAddEvent(final PublishTrendImgViewHolder holder) {
        if (mOnAddClickLister != null) {
            holder.bg_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnAddClickLister.OnAddClick(holder.bg_iv, layoutPosition);

                }
            });
            holder.bg_iv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnAddClickLister.OnAddLongClick(holder.bg_iv, layoutPosition);
                    return false;
                }
            });
        }
    }


}

class PublishTrendImgViewHolder extends RecyclerView.ViewHolder {
    ImageView item_publish_iv, item_publish_cancel_iv, bg_iv;

    public PublishTrendImgViewHolder(View itemView) {
        super(itemView);
        bg_iv = (ImageView) itemView.findViewById(R.id.item_publish_defult_iv);
        item_publish_iv = (ImageView) itemView.findViewById(R.id.item_publish_iv);
        item_publish_cancel_iv = (ImageView) itemView.findViewById(R.id.item_publish_cancel_iv);
    }
}