package com.ifavine.pay.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ifavine.pay.R;

import java.util.ArrayList;
import java.util.List;


public class BMutilDialog extends android.app.Dialog implements View.OnClickListener {


    private Context mContext;
    private View view;
    private OnClickCallBack callBack;
    private List<TextView> textViews;

    private BMutilDialog(Context context, int defStyle) {
        super(context, defStyle);
        view = getLayoutInflater().inflate(R.layout.dialog_mutil, null);
        super.setContentView(view);
    }


    public BMutilDialog(Context context, OnClickCallBack callBack, int[] images, String[] name) {
        this(context, R.style.logout_dialog_bottom);
        this.mContext = context;
        View v1 = (View) view.findViewById(R.id.v1);
        View v2 = (View) view.findViewById(R.id.v2);
        View v3 = (View) view.findViewById(R.id.v3);

        TextView tv1 = (TextView) view.findViewById(R.id.tv1);
        TextView tv2 = (TextView) view.findViewById(R.id.tv2);
        TextView tv3 = (TextView) view.findViewById(R.id.tv3);
        TextView tv4 = (TextView) view.findViewById(R.id.tv4);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv2.setVisibility(View.GONE);
        tv3.setVisibility(View.GONE);
        tv4.setVisibility(View.GONE);
        textViews = new ArrayList<>();
        textViews.add(tv1);
        textViews.add(tv2);
        textViews.add(tv3);
        textViews.add(tv4);
        for (int i = 0; i < name.length; i++) {
            textViews.get(i).setText(name[i]);
            textViews.get(i).setVisibility(View.VISIBLE);
            Drawable dra = context.getResources().getDrawable(images[i]);
            dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
            textViews.get(i).setCompoundDrawables(dra, null, null, null);
            if (i == 1) {
                v1.setVisibility(View.VISIBLE);
            }
            if (i == 2) {
                v2.setVisibility(View.VISIBLE);
            }
            if (i == 3) {
                v3.setVisibility(View.VISIBLE);
            }
        }
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.95);
        getWindow().setAttributes(p);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:
                dismiss();
                callBack.call(0);
                break;
            case R.id.tv2:
                dismiss();
                callBack.call(1);
                break;
            case R.id.tv3:
                dismiss();
                callBack.call(2);
                break;
            case R.id.tv4:
                dismiss();
                callBack.call(3);
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    public interface OnClickCallBack {
        void call(int index);
    }
}