package com.fxy.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.elvishew.xlog.XLog;
import com.fxy.R;
import com.fxy.baselibrary.views.dialog.RxDialogZoomImageView;
import com.fxy.detection.bean.OptionsBean;
import com.fxy.view.FullyGridLayoutManager;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

import java.util.List;

public class PictureListAdapter extends BaseQuickAdapter<OptionsBean, BaseViewHolder> {
    private LayoutInflater mInflater;
    private Context context;
    private Activity mActivity;
    private String PictureType = "";
    private String tag = "";//各种特殊标识使用
    private int screenWidth;
    private int screenHeight;
    private  float density;
    private int spanCount;

    private View errorView;     //错误显示页
    private View emptyView;     //空显示页
    private RecyclerView recyclerView;

    private  onAddPicClickListener mOnAddPicClickListener;

    public interface onAddPicClickListener {
        void onAddPicClick(String PictureType);
    }
    public void setAddPicClick(onAddPicClickListener listener) {
        this.mOnAddPicClickListener = listener;
    }

    //
//    private OnItemClickListener mItemClickListener;
//
//    public interface OnItemClickListener {
//        void onItemClick(int position, View v,String PictureType);
//    }
//
//    public void setOnItemClickListener(OnItemClickListener listener) {
//        this.mItemClickListener = listener;
//    }

    public PictureListAdapter(Context context, Activity activity, RecyclerView view, List<OptionsBean> list, String type){
        super(R.layout.item_preview_image, list);
        this.context = context;
        this.mActivity = activity;
        mInflater = LayoutInflater.from(context);
        recyclerView = view;
        PictureType  = type;

        emptyView = mInflater.inflate(R.layout.pager_empty, (ViewGroup) recyclerView.getParent(), false);
        errorView = mInflater.inflate(R.layout.pager_error, (ViewGroup) recyclerView.getParent(), false);
        setEmptyView(emptyView);
        openLoadAnimation(BaseQuickAdapter.SCALEIN);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenHeight = displayMetrics.heightPixels; // 屏幕高度（像素）
        screenWidth = displayMetrics.widthPixels; // 屏幕高度（像素）
        density = activity.getResources().getDisplayMetrics().density;

        spanCount = 3;
        if (Math.max(screenHeight,screenWidth)>1000){
            spanCount = (int) screenWidth/500;
        }



        recyclerView.setLayoutManager(new FullyGridLayoutManager(mContext, spanCount, GridLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(this);


    }

    public void setPictureType(String type){
        PictureType  = type;
    }
    public void setTag(String str){
        tag  = str;
    }


//    public void setNewData( List<OptionsBean> list){
//        super(list);
//    }


    @Override
    protected void convert(BaseViewHolder helper, OptionsBean item) {
        //定义setOnItemChildClickListener 事件点击
        helper.addOnClickListener(R.id.cb_select);
        helper.addOnClickListener(R.id.ivp_img);
        helper.addOnClickListener(R.id.tv_name);

         ImageView  mImg = helper.getView(R.id.ivp_img);
         CheckBox cbSelect = helper.getView(R.id.cb_select);
         TextView tvName = helper.getView(R.id.tv_name);
        RelativeLayout llImgCenter = helper.getView(R.id.ll_img_center);


        // 设置宽度为 200 像素
        if (Math.max(screenHeight,screenWidth)>1000){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) llImgCenter.getLayoutParams();

            layoutParams.width = getDpValue(100,density);
            layoutParams.setMargins(10,10,10,10);
            llImgCenter.setLayoutParams(layoutParams);


            // 获取ImageView的当前LayoutParams（假设它是RelativeLayout的子视图）
//            RelativeLayout.LayoutParams imgParams = (RelativeLayout.LayoutParams) mImg.getLayoutParams();
//            // 设置新的宽度，并保持高度不变（WRAP_CONTENT）
//            imgParams.width = getDpValue(200,density); // 或者你可以使用 layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
//            imgParams.height = getDpValue(200,density); // 或者你可以使用 layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
//            mImg.setLayoutParams(layoutParams);


        }



        tvName.setVisibility(View.VISIBLE);
        tvName.setText(item.getTitle());

         if ( !item.getNeedPhoto().isEmpty()){
             mImg.setVisibility(View.VISIBLE);
             cbSelect.setVisibility(View.VISIBLE);
             cbSelect.setChecked((boolean)item.getChecked());

             RequestOptions options = new RequestOptions()
                     .centerCrop()
                     .placeholder(R.color.color_f6)
                     .diskCacheStrategy(DiskCacheStrategy.ALL);

             Glide.with(helper.itemView.getContext())
                     .load(item.getNeedPhoto())
                     .apply(options)
                     .into(mImg);

         }else{
             mImg.setVisibility(View.GONE);
             cbSelect.setVisibility(View.VISIBLE);
         }



        cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                RelativeLayout parentLinearLayout = (RelativeLayout) buttonView.getParent();

                View overlayView =  parentLinearLayout.findViewById(R.id.overlay_view);

                // 当checkbox1的选中状态改变时执行
                if (isChecked) {
                    overlayView.setVisibility(View.VISIBLE);
                } else {
                    overlayView.setVisibility(View.GONE);
                }
            }
        });

        mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                XLog.e("--------setOnClickListener------");
                int position = RecyclerView.NO_POSITION;
                position = Math.max(position, 0);
//                List<LocalMedia> mediasList = new ArrayList<>();
//                for (int i = 0; i < mData.size(); i++) {
//                    LocalMedia media = new LocalMedia (mData.get(i).getUrl(), 0, PictureConfig.TYPE_IMAGE, "");
//                    media.setCut(false);
//                    mediasList.add(media);
//
//                }
//                com.luck.picture.lib.PictureSelector.create(mActivity).externalPicturePreview(position, mediasList);

                RxDialogZoomImageView rxDialogZoomImageView = new RxDialogZoomImageView(mContext);
//                rxDialogZoomImageView.setImagePath(mData.get(position));
                rxDialogZoomImageView.show();

            }
        });


    }

    private int getDpValue(int dpValue,float density){
        return Math.round((float)dpValue * density);
    }

    public void authCamera() {
        //拍照
        com.luck.picture.lib.PictureSelector.create(mActivity)
                .openCamera(PictureMimeType.ofImage())
                //只需要拍照
                //.selectionMode(PictureConfig.SINGLE)
//                .isCamera(true)
                //.compressMaxKB()//压缩最大值kb compressGrade()为Luban.CUSTOM_GEAR有效
                .compress(true) // 压缩
                .cropCompressQuality(100)// 裁剪压缩质量 默认100
                .minimumCompressSize(100) //最新压缩图片 默认100
                .forResult(PictureConfig.REQUEST_CAMERA);
    }
}
