package com.sjkj.base_lib.weight;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import com.gyf.immersionbar.ImmersionBar;
import com.sjkj.base_lib.R;

public class LoadingDialog extends Dialog {
    public static final int LOADING_SUCCESS = 0;
    public static final int LOADING_FAILED = -1;

    private Context mContext;

    public LoadingDialog(Context context) {
        super(context, R.style.LoadingDialogStyle);
        this.mContext = context;
        initDialog();
    }


    /**
     * 初始化Dialog
     */
    public void initDialog() {
        Window win = getWindow();
        //ImmersionBar.getStatusBarHeight(this);
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        win.getDecorView().setPadding(0, 0, 0, 0);
        win.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION | Gravity.CENTER);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        if (ImmersionBar.getStatusBarHeight(mContext) != 0 && outMetrics.heightPixels != 0) {
            lp.height = outMetrics.heightPixels-ImmersionBar.getStatusBarHeight(mContext);
        } else {
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        }

        win.setAttributes(lp);
        setCanceledOnTouchOutside(false);
    }

    public void showDialog() {
        show();
    }

    /**
     * 隐藏Dialog
     */
    public void hideDialog() {
        try {
            cancel();
            dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_loading);
//        RectangleImageView imageView = findViewById(R.id.imageView);
//        ProgressBar progressBar=findViewById(R.id.progressBar);
//        Animation rotateAnimation = AnimationUtils.loadAnimation(App.getInstance(), R.anim.progress);
//        imageView.startAnimation(rotateAnimation);

//        RectangleImageView imageView = findViewById(R.id.imageView);
//        Glide.with(App.getInstance()).load(R.drawable.icon_loading).asGif().into(imageView);
//        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE);
//        Glide.with(mContext).load(R.drawable.icon_loading_gif).apply(options).into(imageView);
//        try {
//            GifImageView gifImageView1 = (GifImageView) findViewById(R.id.imageView);
//            // 如果加载的是gif动图，第一步需要先将gif动图资源转化为GifDrawable
//            // 将gif图资源转化为GifDrawable
//            GifDrawable gifDrawable = new GifDrawable(this.mContext.getResources(), R.drawable.icon_loading_gif);
//
//            // gif1加载一个动态图gif
//            gifImageView1.setImageDrawable(gifDrawable);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }


    /**
     * 判断当前用户触摸是否超出了Dialog的显示区域
     *
     * @param context
     * @param event
     * @return
     */
    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }


}
