package com.sxyz.rfid.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.sxyz.rfid.App;
import com.sxyz.rfid.AppConfig;
import com.sxyz.rfid.R;
import com.sxyz.rfid.utils.LocalUtils;

/**
 * class ：---
 * author：York(wuchunyuan)
 * time  : 2018/4/4 16:54
 */
public class DialogUtil {
    private static Dialog mDialog;

    /**
     * @description 投资对比选择公司弹窗
     * @author wuchunyuan
     * @time 2020/7/13 13:08
     */
    public static Dialog showIpDialog(Activity activity) {
        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        LayoutInflater inflater = LayoutInflater.from(activity);
        mDialog = new Dialog(activity, R.style.AlertDialogStyle);
        View layout = inflater.inflate(R.layout.dialog_ip_layout, null);
        LinearLayout rlLayout = layout.findViewById(R.id.lLayout_bg);
        TextView txtIp = layout.findViewById(R.id.txt_ip);
        txtIp.setText("当前IP:  " + (TextUtils.isEmpty(App.ip) ? AppConfig.BASE_URL : LocalUtils.getIpPort()));
        ClearEditText etIp = layout.findViewById(R.id.et_ip);
        ClearEditText etPort = layout.findViewById(R.id.et_port);
        Button etOk = layout.findViewById(R.id.bt_ok);
        Button btCancel = layout.findViewById(R.id.bt_cancel);
        etOk.setOnClickListener(v -> {
            if (TextUtils.isEmpty(etIp.getText().toString())) {
                ToastUtils.showShort("请输入ip");
                return;
            }
            if (TextUtils.isEmpty(etPort.getText().toString())) {
                ToastUtils.showShort("请输入端口");
                return;
            }
            if (mOnSwitchItemClickListener != null) {
                mOnSwitchItemClickListener.onSwitchItemClick(etIp.getText().toString(), etPort.getText().toString());
            }
        });
        btCancel.setOnClickListener(v -> mDialog.dismiss());
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        mDialog.addContentView(layout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(true);
        mDialog.setContentView(layout);
        rlLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT));
        mDialog.show();
        return mDialog;
    }

    public interface OnSwitchItemClickListener {
        void onSwitchItemClick(String ip, String port);
    }

    private static OnSwitchItemClickListener mOnSwitchItemClickListener;

    public void setOnSwitchItemClickListener(OnSwitchItemClickListener mOnSwitchItemClickListener) {
        this.mOnSwitchItemClickListener = mOnSwitchItemClickListener;
    }
}
