package com.android.chmo.ui.dialog;

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

import com.android.chmo.R;
import com.android.chmo.utils.PixelUtils;


public class ConfirmDialog extends Dialog {
    private String msg;
    private String okStr;
    private String cancelStr;
    private DialogListener listener;

    public ConfirmDialog(Context context) {
        super(context, R.style.DialogStyle);
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if(listener != null) {
                    listener.onCancel(ConfirmDialog.this);
                }
            }
        });
    }

    public ConfirmDialog setOutTouchDismiss(boolean flag) {
        setCanceledOnTouchOutside(flag);
        return this;
    }

    public ConfirmDialog setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ConfirmDialog setDialogListener(DialogListener listener) {
        this.listener = listener;
        return this;
    }

    public ConfirmDialog setButton(String ok, String cancel) {
        this.okStr = ok;
        this.cancelStr = cancel;
        return this;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_confirm);
        Window window = getWindow();
         WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = PixelUtils.getWindowWidth();
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);

        TextView msgView = (TextView) findViewById(R.id.msg);
        TextView okBtn = (TextView) findViewById(R.id.ok);
        TextView cancelBtn = (TextView) findViewById(R.id.cancel);
        if (!TextUtils.isEmpty(msg)) {
            msgView.setText(msg);
        }
        if (!TextUtils.isEmpty(okStr)) {
            okBtn.setText(okStr);
        }
        if (!TextUtils.isEmpty(cancelStr)) {
            cancelBtn.setText(cancelStr);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.onCancel(ConfirmDialog.this);
                }
            }
        });

        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.onOk(ConfirmDialog.this);
                }
            }
        });
    }

    public interface DialogListener {
        void onOk(Dialog dialog);
        void onCancel(Dialog dialog);
    }
}
