package com.ycgis.macall.personalcenter.v.photoview;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.ycgis.macall.personalcenter.R;

/**
 * @Author macall
 * @Create 2020/5/19 0019
 * @Describe
 */
public class ShowPhotoDialog extends Dialog {

    public ShowPhotoDialog(@NonNull Context context) {
        super(context);
    }

    public ShowPhotoDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_show_photo);
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity= Gravity.CENTER;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

    public void show(String photoUrl) {
        show();
        PhotoView photoView = findViewById(R.id.pv_img);
        photoView.setScaleType(ImageView.ScaleType.FIT_XY);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        Glide.with(getContext()).load(photoUrl).
                apply(new RequestOptions()
                        .error(R.drawable.ic_load_imag_eerror))
                .into(photoView);
    }

    public void show(Drawable photoDrawable) {
        show();
        PhotoView photoView = findViewById(R.id.pv_img);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (photoDrawable == null){
            photoView.setImageDrawable(ContextCompat.getDrawable(getContext(),R.drawable.ic_load_imag_eerror));
        }else {
            photoView.setImageDrawable(photoDrawable);
        }
//        Glide.with(getContext()).load(photoDrawable).
//                apply(new RequestOptions()
//                        .error(R.drawable.ic_load_imag_eerror)
//                        .diskCacheStrategy(DiskCacheStrategy.DATA)
//                        .skipMemoryCache(true)).into(photoView);

    }

}
