package com.sjkj.appthreefloor_tsgz.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
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 android.widget.TextView;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.base_lib.utils.CommonUtils;

/**
 * Author       menc
 * Desc	        ${更新Dialog}
 */
public class UpdateDialog extends Dialog {
    public static final int LOADING_SUCCESS = 0;
    public static final int LOADING_FAILED = -1;

    private Context mContext;
    private TextView localVersionTv;
    private TextView newVersionTv;
    private TextView msgTv;
    private boolean isCancel = true;
    private TextView cancelTv;

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

    /**
     * 初始化Dialog
     */
    public void initDialog() {
        Window win = getWindow();
        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;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        setCancelable(isCancel);
        setCanceledOnTouchOutside(isCancel);
    }

    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_update);
        initView();
    }

    private void initView() {
        localVersionTv = findViewById(R.id.local_version_tv);
        newVersionTv = findViewById(R.id.new_version_tv);
        msgTv = findViewById(R.id.msg_tv);
        TextView updateTv = findViewById(R.id.update_tv);
        cancelTv = findViewById(R.id.cancel_tv);
        updateTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideDialog();
                if (mListener != null) mListener.onItemClick(1);
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideDialog();
                if (mListener != null) mListener.onItemClick(0);
            }
        });
    }

    public void setParams(String localVersion, String newVersion, String msg, boolean isCancel) {
        if (localVersionTv != null)
            this.localVersionTv.setText(CommonUtils.stringNotEmpty(localVersion));
        if (newVersionTv != null)
            this.newVersionTv.setText(CommonUtils.stringNotEmpty(newVersion));
        if (msgTv != null)
            this.msgTv.setText(CommonUtils.stringNotEmpty(msg));
        if (cancelTv != null){
            this.cancelTv.setClickable(isCancel);
            this.cancelTv.setVisibility(isCancel ? View.VISIBLE : View.INVISIBLE);
            setCancelable(isCancel);
            setCanceledOnTouchOutside(isCancel);
        }
    }

    /**
     * 判断当前用户触摸是否超出了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));
    }

    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        /**
         * 点击条目
         *
         * @param type 0取消，1 first，2 second
         */
        void onItemClick(int type);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


}
