package com.sybotan.android.demo.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.sybotan.android.demo.R;
import com.sybotan.android.demo.activities.LookBigPicActivity;
import cn.sagacloud.android.cadengine.types.Photos;
import com.sybotan.android.demo.retrofit.Constant;
import com.sybotan.android.demo.tools.CommonUtils;
import com.sybotan.android.demo.tools.DensityUtils;
import com.sybotan.android.demo.tools.GlideApp;
import com.tencent.smtt.sdk.ui.dialog.widget.RoundImageView;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * created by lihao on 2018/12/11
 * 九图
 */
public class PictureAdapter extends BaseRecyclerViewAdapter<Photos> {
    private int width;
    private int currentPosition;
    private String id;
    private boolean canEdit;
    private PicAdd picAdd;

    public PictureAdapter(Context context, List<Photos> beans, String id, boolean canEdit, int currentPosition, int width) {
        super(context, beans);
        this.id = id;
        this.canEdit = canEdit;
        this.currentPosition = currentPosition;
        this.width = width;
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.item_node_nine_pic;
    }

    @Override
    protected void onBindDataToView(ViewHolder holder, Photos photos) {
        ViewGroup.LayoutParams layoutParams = holder.getView(R.id.node_nine_pic_iv).getLayoutParams();
        layoutParams.width = DensityUtils.dp2px(mContext, width);
        layoutParams.height = DensityUtils.dp2px(mContext, width);
        holder.getView(R.id.node_nine_pic_iv).setLayoutParams(layoutParams);
        switch (photos.itemType) {
            case 0:
                if (canEdit) {
                    holder.setVisible(R.id.node_nine_pic_delete);
                    holder.getView(R.id.node_nine_pic_delete).setOnClickListener(v -> {
                        if (!CommonUtils.isFastDoubleClick()) {
//                            EventBus.getDefault().post(new DeletePicEvent(id, photos.key, currentPosition, photos.path));
                        }
                    });
                } else {
                    holder.setInVisible(R.id.node_nine_pic_delete);
                }
                String url = Constant.ServerImgIPDown + photos.key;
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(photos.path);
                    Bitmap bitmap = BitmapFactory.decodeStream(fis);
                    ImageView imageView = (ImageView) holder.getView(R.id.node_nine_pic_iv);
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    loadImageViewCornersByPath(mContext, url, holder.getView(R.id.node_nine_pic_iv));
                }

                holder.getView(R.id.node_nine_pic_iv).setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, LookBigPicActivity.class);
                    intent.putExtra("url", url);
                    intent.putExtra("key", photos.key);
                    intent.putExtra("id", id);
                    intent.putExtra("canEdit", canEdit);
                    intent.putExtra("position", currentPosition);
                    mContext.startActivity(intent);
                });
                holder.setText(R.id.node_nine_pic_position, holder.getCurrentPosition() + ".");
                break;
            case 2:
                if (canEdit) {
                    holder.setVisible(R.id.node_nine_pic_delete);
                    holder.getView(R.id.node_nine_pic_delete).setOnClickListener(v -> {
                        if (!CommonUtils.isFastDoubleClick()) {
//                            EventBus.getDefault().post(new DeletePicEvent(id, photos.key, currentPosition, photos.path));
                        }
                    });
                } else {
                    holder.setInVisible(R.id.node_nine_pic_delete);
                }
                holder.setText(R.id.node_nine_pic_position, holder.getCurrentPosition() + 1 + ".");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                photos.bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] bytes = baos.toByteArray();
                RoundImageView view = holder.getView(R.id.node_nine_pic_iv);
                view.setImageBitmap(photos.bitmap);
                holder.getView(R.id.node_nine_pic_iv).setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, LookBigPicActivity.class);
                    intent.putExtra("path", photos.path);
                    intent.putExtra("key", photos.key);
//                    intent.putExtra("id", id);
                    intent.putExtra("canEdit", canEdit);
                    intent.putExtra("position", currentPosition);
                    mContext.startActivity(intent);
                });
                break;
            case 1:
                holder.setText(R.id.node_nine_pic_position, "");
                holder.setInVisible(R.id.node_nine_pic_delete);
                holder.getView(R.id.node_nine_pic_iv).setOnClickListener(v -> {
                    if (picAdd != null) {
                        picAdd.onPicAdd();
                    }
                });
                break;
        }
    }

    public void loadImageViewCornersByPath(Context mContext, String path, ImageView mImageView) {
        if (TextUtils.isEmpty(path) || (path.contains("key=null") || path.endsWith("key="))) {
            mImageView.setImageResource(R.mipmap.image_error_icon);
            return;
        }
        GlideApp.with(mContext)
                .load(path)
                .into(mImageView);
    }

    @Override
    public int getItemViewType(int position) {
        return mBeans.get(position).itemType;
    }

    public interface PicAdd {
        void onPicAdd();
    }

    public void setOnPicAdd(PicAdd picAdd) {
        this.picAdd = picAdd;
    }
}
