package com.example.administrator.zhapp.weight;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
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 android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.zhapp.R;

/**
 * Created by Rehome-rjb1 on 2017/5/31.
 */

public class CommitDialog extends Dialog {

    TextView tvMsg;
    TextView dialogCancel;
    TextView dialogCommit;
    LinearLayout layDialogCancel;

    private CommitDialogListener listener;

    public CommitDialog(@NonNull Context context, CommitDialogListener listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(R.layout.layout_commit_dialog);
        findView();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (getScreenWidth(context)) * 2 / 3;
        window.setGravity(Gravity.CENTER);
        this.listener = listener;
        setCancelable(false);
    }

    private void findView(){

        tvMsg = findViewById(R.id.tv_msg);
        dialogCancel = findViewById(R.id.dialog_cancel);
        dialogCommit = findViewById(R.id.dialog_commit);
        layDialogCancel = findViewById(R.id.lay_dialog_cancel);
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.cancel();
                }
            }
        });
        dialogCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
    }

    public void setTvMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            tvMsg.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 CommitDialogListener {
        void confirm();

        void cancel();
    }
}
