package com.zxhl.cms.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zxhl.cms.AppContext;
import com.zxhl.cms.R;
import com.zxhl.cms.utils.CornerTransform;
import com.zxhl.cms.utils.Utils;

/**
 * Created by gaoleichao on 2018/5/24.
 */

public class RecycleImageView extends AppCompatImageView {
    private Object imageUrl;
    private int mHolderResId;
    private int dp;
    private boolean isRound = false;
    private boolean isCenterCrop = false;
    private boolean isResize = false;
    private RequestBuilder mBitmapRequestBuilder;
    private RequestOptions mRequestOptions;
    private Context mContext;

    public RecycleImageView(Context context) {
        super(context);
        init(context);
    }

    public RecycleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RecycleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        mRequestOptions = new RequestOptions();
        mContext = context;
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        setImageDrawable(null);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        loadImage();
    }

    public void setLoadImageUrl(Object url, boolean isCenterCrop) {
        imageUrl = url;
        mHolderResId = -1;
        this.isRound = false;
        this.isCenterCrop = isCenterCrop;
        dp = 0;
        if (imageUrl == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isAttachedToWindow()) {
                loadImage();
            }
        } else {
            loadImage();
        }
    }

    public void setLoadImageUrl(Object url, int holderResId, boolean isRound) {
        imageUrl = url;
        mHolderResId = holderResId;
        this.isRound = isRound;
        dp = 0;
        if (imageUrl == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isAttachedToWindow()) {
                loadImage();
            }
        } else {
            loadImage();
        }
    }

    public void setLoadImageUrl(Object url, int holderResId, int dp) {
        imageUrl = url;
        mHolderResId = holderResId;
        this.isRound = true;
        this.dp = dp;
        this.isResize = isResize;
        if (imageUrl == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isAttachedToWindow()) {
                loadImage();
            }
        } else {
            loadImage();
        }
    }

    private void loadImage() {
//        #
//        CornerTransform transform = new CornerTransform(mContext, Utils.dip2px(dp));
//        transform.setExceptCorner(false, false, false, false);
//        Glide.with(AppContext.get()).asBitmap().load(imageUrl).placeholder(R.drawable.img_default).transform(transform).into(this);

        if (mContext == null) {
            return;
        }
        if (imageUrl instanceof String) {
        }
        mBitmapRequestBuilder = Glide.with(AppContext.get()).asBitmap().load(imageUrl);
        if (isCenterCrop) {
            mRequestOptions.centerCrop();
        } else {
            mRequestOptions.fitCenter();
        }
        mRequestOptions.skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        if (mBitmapRequestBuilder == null) {
            return;
        }
        if (mRequestOptions != null) {
//            mRequestOptions.error(mHolderResId).placeholder(mHolderResId).dontAnimate();
            mRequestOptions.error(mHolderResId).dontAnimate();
        }
        if (isRound) {
            CornerTransform transform = new CornerTransform(mContext, Utils.dip2px(dp));
            transform.setExceptCorner(false, false, false, false);
            mRequestOptions.transform(transform);
            mBitmapRequestBuilder.apply(mRequestOptions).into(this);
        } else {
            BitmapTransitionOptions options = BitmapTransitionOptions.withCrossFade(200);
            mBitmapRequestBuilder.transition(options).apply(mRequestOptions).into(this);
        }
    }

}

