package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.tofly.ghrq_route.R;

/**
 * Created by zombiexq on 2016/6/15.
 */
public class ConfirmDialog extends AppCompatDialog {

    private Context context;
    private String title;
    private String message;
    private Button btnok, btncancle;
    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        public void doOk();
        public void doCancel();
    }

    public ConfirmDialog(Context context, String title, String message) {
        //super(context, R.style.dialog);
        super(context,  R.style.dialog);
        this.context = context;
        this.title = title;
        this.message = message;
    }

    public ConfirmDialog(Context context, String message) {
        super(context, R.style.dialog);
        this.context = context;
        this.title = "提示";
        this.message = message;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_confirm, null);
        setContentView(view);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_dialog_title);
        tvTitle.setText(title);

        TextView tvMessage = (TextView) view.findViewById(R.id.tv_dialog_message);
        tvMessage.setText(message);

        btncancle = (Button) findViewById(R.id.tv_dialog_cancel);
        btnok = (Button) findViewById(R.id.tv_dialog_ok);

        btnok.setOnClickListener(new clickListener());
        btncancle.setOnClickListener(new clickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.tv_dialog_ok:
                    clickListenerInterface.doOk();
                    ConfirmDialog.this.dismiss();
                    break;
                case R.id.tv_dialog_cancel:
                    clickListenerInterface.doCancel();
                    ConfirmDialog.this.dismiss();
                    break;
            }
        }

    }

}