package com.android.chmo.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

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

/**
 * Created by Administrator on 2018/8/6.
 */

public class SelectDialog extends Dialog {

    private LinearLayout container;

    private String[] items;

    private ItemListener itemListener;


    public SelectDialog(Context context) {
        super(context, R.style.DialogStyle);
    }

    public SelectDialog setItems(String[] items) {
        this.items = items;
        return this;
    }

    public SelectDialog setItemListener(ItemListener listener) {
        this.itemListener = listener;
        return this;
    }


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

        container = (LinearLayout) findViewById(R.id.container);


        if(items != null) {
            for (int i=0; i < items.length; i++) {
                container.addView(getItem(items[i],i));
            }
        }

        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private View getItem(String item, final int pos) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.select_item,null);
        TextView textView = (TextView) view.findViewById(R.id.item);
        textView.setText(item);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(itemListener != null) {
                    itemListener.onSelect(pos);
                }
            }
        });
        return view;
    }



    public interface ItemListener {

        void onSelect(int pos);
    }
}
