package com.hainaos.vc.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hainaos.vc.R;
import com.hainaos.vc.activity.preview.PreviewActivity;
import com.hainaos.vc.bean.PhotoInfo;
import com.hainaos.vc.utils.JgyUtils;
import com.hainaos.vc.utils.ScreenUtils;
import com.hainaos.vc.utils.VideoUtils;
import com.shehuan.niv.NiceImageView;

import java.util.ArrayList;

public class PicAdapter extends RecyclerView.Adapter<PicAdapter.Holder> {

    private Context mContext;
    private GridLayoutManager mGridLayoutManager;
    private ArrayList<PhotoInfo> mPhotoInfos;

    public PicAdapter(GridLayoutManager gridLayoutManager) {
        this.mGridLayoutManager = gridLayoutManager;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        Holder holder = new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_pic, parent, false));
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.iv_pic.getLayoutParams();
        //动态计算，设置item的宽高一致，总宽度-左右margin-左右padding /总列数-item左右margin-item左右padding
//        params.height =
//                (mGridLayoutManager.getWidth() - layoutParams.leftMargin
//                        - layoutParams.rightMargin - holder.iv_pic.getPaddingStart() - holder.iv_pic.getPaddingEnd()) / mGridLayoutManager.getSpanCount()
//                        - 2 * holder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) params).leftMargin;
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        PhotoInfo photoInfo = mPhotoInfos.get(position);
        String fileName = photoInfo.getFile_name();
        String fileUrl = photoInfo.getFile();
        if (TextUtils.isEmpty(fileName)) {
            holder.tv_name.setText(VideoUtils.getFileNameWithoutExtension(fileUrl));
        } else {
            holder.tv_name.setText(fileName);
        }
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(ScreenUtils.dip2px(mContext, 4F)));
        if (!JgyUtils.getInstance().fileExists(photoInfo.getFile())) {
//            Glide.with(mContext).load(fileUrl).into(holder.iv_pic);
            loadImg(fileUrl, holder.iv_pic);
        } else {
//            Glide.with(mContext).load(JGYUtils.getInstance().getUrlLocalPath(photoInfo.getFile())).into(holder.iv_pic);
            loadImg(JgyUtils.getInstance().getUrlLocalPath(photoInfo.getFile()), holder.iv_pic);
        }
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PreviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("mPhotoInfos", mPhotoInfos);
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    private void loadImg(String path, ImageView imageView) {
        Glide.with(imageView).asBitmap().load(path).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                int imgWidth = resource.getWidth();
                int imgHeight = resource.getHeight();
                imageView.setImageBitmap(resource);
//                Log.e("into", "onResourceReady: width = " + imgWidth + "height = " + imgHeight);
//                ViewGroup.LayoutParams layoutParams = iv_img.getLayoutParams();
//                int width = iv_img.getWidth();
//                layoutParams.height = (int) (imgHeight * (1.0f * width /imgWidth  ));
//                iv_img.setLayoutParams(layoutParams);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return mPhotoInfos == null ? 0 : mPhotoInfos.size();
    }

    public void setPhotoInfos(ArrayList<PhotoInfo> photoInfos) {
        this.mPhotoInfos = photoInfos;
        notifyDataSetChanged();
    }

    class Holder extends RecyclerView.ViewHolder {
        ConstraintLayout root;
        NiceImageView iv_pic;
        TextView tv_name;

        public Holder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            iv_pic = itemView.findViewById(R.id.iv_pic);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }
}
