package aicare.net.cn.sdk.ailinksdkdemoandroid.modules.barometric_temp_humidity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import aicare.net.cn.sdk.ailinksdkdemoandroid.R;


public class EditParamsDialog extends DialogFragment implements View.OnClickListener {

    /**
     * 是否显示
     */

    private EditText etContent1, etContent2, etContent3;
    private TextView mTvCancel, mTvSucceed;
    private boolean mShow;
    private boolean mCancelBlank;
    private Context mContext;
    private OnDialogListener mOnDialogListener;




    public static EditParamsDialog newInstance() {
        return new EditParamsDialog();
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialogView = new Dialog(requireContext(), R.style.MyDialog);// 创建自定义样式dialog
        dialogView.setCancelable(false);//设置是否可以关闭
        dialogView.setCanceledOnTouchOutside(mCancelBlank);//设置点击空白处是否可以取消
        dialogView.setOnKeyListener((dialog, keyCode, event) -> {
            if (mCancelBlank) {
                return false;
            } else {
                //返回不关闭
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        return dialogView;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_edit_params, container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContext = view.getContext();
        init(view);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    private void init(View view) {
        etContent1 = view.findViewById(R.id.et_context1);
        etContent2 = view.findViewById(R.id.et_context2);
        etContent3 = view.findViewById(R.id.et_context3);
        mTvCancel = view.findViewById(R.id.tv_cancel);
        mTvSucceed = view.findViewById(R.id.tv_ok);
        mTvCancel.setOnClickListener(this);
        mTvSucceed.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_cancel) {
            if (mOnDialogListener != null) {
                mOnDialogListener.tvCancelListener(v);
            }
            dismiss();
        } else if (i == R.id.tv_ok) {
            if (mOnDialogListener != null) {
                String content1 = etContent1.getText().toString().trim();
                String content2 = etContent2.getText().toString().trim();
                String content3 = etContent3.getText().toString().trim();
                int value1 = 1;
                int value2 = 30;
                int value3 = 20;
                try {
                    value1 = Integer.parseInt(content1);
                    if (value1 > 10 || value1 < 1) {
                        Toast.makeText(mContext, "采样频率输入有误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(mContext, "采样频率输入有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    value2 = Integer.parseInt(content2);
                    if (value2 > 60 || value2 < 1) {
                        Toast.makeText(mContext, "保存频率输入有误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(mContext, "保存频率输入有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    value3 = Integer.parseInt(content3);
                    if (value3 > 20 || value3 < 2) {
                        Toast.makeText(mContext, "设备定时器间隔输入有误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(mContext, "设备定时器间隔输入有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                mOnDialogListener.tvSucceedListener(v, value1, value2, value3);
            }
            dismiss();
        }
    }

    public EditParamsDialog setOnDialogListener(OnDialogListener onDialogListener) {
        mOnDialogListener = onDialogListener;
        return this;
    }

    /**
     * 当前是否显示
     */
    public boolean isShow() {
        return mShow;
    }


    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        try {
            if (!mShow) {
                super.show(manager, tag);
                mShow = true;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
    }


    public void show(@NonNull FragmentManager manager) {
        this.show(manager, "DialogFragment");
    }


    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        mShow = false;
    }


    @Override
    public void dismiss() {
        try {
            mShow = false;
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnDialogListener {

        /**
         * 取消的点击事件
         */
        default void tvCancelListener(View v) {
        }

        /**
         * 成功的点击事件
         */
        default void tvSucceedListener(View v, int value1, int value2, int value3) {
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mShow = false;
    }


}
