package com.rehome.meetingbook.weiget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.rehome.meetingbook.bean.AppointmentMemberBean;
import com.rehome.meetingbook.databinding.DialogChangeBzBinding;
import com.rehome.meetingbook.databinding.DialogResultBinding;

/**
 * Create By HuangWenFei
 * 创建日期：2022-12-19 09:19
 * 描述：
 */
public class ChangeBzDialog extends Dialog {
    protected View mView;
    protected DialogChangeBzBinding binding;
    Context context;
    private ChangeBzDialogListener listener;
    private AppointmentMemberBean.RowsBean item;

    public ChangeBzDialog(@NonNull Context context, String msg, AppointmentMemberBean.RowsBean item, ChangeBzDialogListener listener) {
        super(context);
        this.context=context;
        this.item=item;
        binding = DialogChangeBzBinding.inflate(getLayoutInflater());
        mView = binding.getRoot();
        binding.tvTitle.setText(msg);
        binding.etIstz.setText(item.getIstzzy());
        binding.etBeizhu.setText(item.getBz());

        binding.bottomBtnView.dialogCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    item.setIstzzy(binding.etIstz.getText().toString().trim());
                    item.setBz(binding.etBeizhu.getText().toString().trim());
                    listener.confirm();
                }
            }
        });
        binding.bottomBtnView.dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.cancel();
                }
            }
        });

        this.listener = listener;
        setCancelable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (getScreenWidth(context))  / 2;
        window.setGravity(Gravity.CENTER);

//        Window window = this.getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        Display d = window.getWindowManager().getDefaultDisplay();
//        lp.width = (int) (d.getWidth() * 0.9F);
//        window.setAttributes(lp);
    }

    public void setTvMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            binding.tvTitle.setText(msg);
        }
    }

    //获取屏幕宽度
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    public interface ChangeBzDialogListener {
        void confirm();
        void cancel();
    }

}
