package com.zhilin.patrol.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.fuyin.constans.P;
import com.fuyin.ui.ImagePreviewActivity;
import com.zhilin.patrol.R;
import com.zhilin.patrol.adapter.helper.MyItemTouchCallback;
import com.zhilin.patrol.tool.DensityUtil;
import com.zhilin.patrol.tool.Utils;
import com.zhilin.patrol.ui.activity.PatrolUploadActivity;
import com.zhilin.patrol.ui.activity.RecifyUploadActivity;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 选择图片适配器
 */
public class UpImageAdapter extends RecyclerView.Adapter<UpImageAdapter.ViewHolder> implements MyItemTouchCallback.ItemTouchAdapter {

    /** 上传图片最大数 */
    public static final int MAX_IMAGE_SIZE = 9;
    private ArrayList<String> mDataList = new ArrayList<>();
    private Context mContext;
    private int mSpanCount;//一列行数

    public UpImageAdapter(Context context, ArrayList<String> dataList, int spanCount) {
        this.mDataList = dataList;
        this.mContext = context;
        this.mSpanCount = spanCount;
    }

    public Object getItem(int position) {
        if (mDataList != null
                && mDataList.size() == MAX_IMAGE_SIZE) {
            return mDataList.get(position);
        } else if (mDataList == null || position - 1 < 0
                || position > mDataList.size()) {
            return null;
        } else {
            return mDataList.get(position - 1);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_publish, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (isShowAddItem(position)) {
            holder.imageIv.setImageResource(R.drawable.btn_add_pic);
            holder.imageIv.setBackgroundResource(R.color.bg_gray);
            holder.delIv.setVisibility(View.GONE);
        } else {
            holder.delIv.setVisibility(View.VISIBLE);
            final String imgPath = mDataList.get(position);
            //            if (isFromPremises || mIsUploadAddress) {
            //                ImageDisplayer.getInstance(mContext).displayBmp(holder.imageIv,
            //                        item.thumbnailPath, item.compressDir);
            //            } else {
            //                ImageDisplayer.getInstance(mContext).displayBmp(holder.imageIv,
            //                        item.thumbnailPath, item.sourcePath);
            //            }
            //Glide加载图片
            final RequestOptions options = new RequestOptions();
            options.centerCrop()
                    .placeholder(R.mipmap.bg_img)
                    .error(R.mipmap.bg_img);
            Glide.with(mContext)
                    .load(imgPath)
//                    .diskCacheStrategy(DiskCacheStrategy.NONE)
//                    .error(R.mipmap.bg_img)
                    .into(holder.imageIv);
            //删除图片
            holder.delIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDataList.remove(imgPath);
                    Utils.delSingleFile(imgPath);
                    notifyDataSetChanged();
                    if (mContext instanceof PatrolUploadActivity) {
                        ((PatrolUploadActivity) mContext).setImageNum();
                    }

                }
            });
        }
        //添加图片和浏览图片点击
        holder.imageIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isShowAddItem(position)) {
//                    PhotoPicker.builder()
//                            .setPhotoCount(9)
//                            .setGridColumnCount(4)
//                            .setSelected(mDataList)
//                            .start((Activity) mContext);
                    ((PatrolUploadActivity) mContext).toCamera();
                } else {
//                    PhotoPreview.builder()
//                            .setPhotos(mDataList)
//                            .setCurrentItem(position)
//                            .start((Activity) mContext);

                    Intent intent = new Intent(mContext, ImagePreviewActivity.class);
                    intent.putStringArrayListExtra("imageList", (ArrayList<String>) mDataList);
                    intent.putExtra(P.START_ITEM_POSITION, position);
                    intent.putExtra(P.START_IAMGE_POSITION, position);
//                        ActivityOptions compat = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView, imageView.getTransitionName());
//                        startActivity(intent, compat.toBundle());
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(0, 0);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        // 多返回一个用于展示添加图标
        if (mDataList == null) {
            return 1;
        } else if (mDataList.size() == MAX_IMAGE_SIZE) {
            return MAX_IMAGE_SIZE;
        } else {
            return mDataList.size() + 1;
        }
    }

    private boolean isShowAddItem(int position) {
        int size = mDataList == null ? 0 : mDataList.size();
        return position == size;
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {
        if (fromPosition == mDataList.size() || toPosition == mDataList.size()) {
            return;
        }
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mDataList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mDataList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {
        mDataList.remove(position);
        notifyItemRemoved(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageIv;
        ImageView delIv;
        View itemView;

        public ViewHolder(View view) {
            super(view);
            this.itemView = view;
            imageIv = (ImageView) itemView.findViewById(R.id.item_grid_image);
            delIv = (ImageView) itemView.findViewById(R.id.item_img_del);
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth() - (mSpanCount + 1) * DensityUtil.dip2px(mContext, 8);
            ViewGroup.LayoutParams layoutParams = imageIv.getLayoutParams();
            layoutParams.height = width / mSpanCount;
            imageIv.setLayoutParams(layoutParams);
        }
    }

}
