package com.revoapp.calendar.dialogs;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.revoapp.calendar.adapter.SimpleDialogAdapter;
import com.revoapp.api.database.calendar.pojo.Events;
import com.revoapp.api.utils.ImageHelper;
import com.revoapp.calendar.DayActivity;
import com.revoapp.calendarapp.R;
import com.revoapp.calendar.helper.SimpleDialogItem;
import com.revoapp.calendar.helper.ThemeHelper;

import java.util.ArrayList;

public class EventsContextDialog extends AbstractDialog {
    private Events event;
    private EventsDeleteConfirmDialog confirmDelete;
    private DayActivity act;

    public EventsContextDialog(final Context context, DayActivity act) {
        super(context, R.layout.dialog_context);

        ArrayList<SimpleDialogItem> items = new ArrayList<SimpleDialogItem>();
        SimpleDialogItem item;
        this.act = act;
        confirmDelete = new EventsDeleteConfirmDialog(context, this);
        item = new SimpleDialogItem(ThemeHelper.get_icon50_menu_delete(),
                context.getString(R.string.delete));
        items.add(item);

        ListView list = (ListView) findViewById(R.id.listView1);
        list.setDivider(context.getResources().getDrawable(
                ThemeHelper.get_separator_list()));
        list.setAdapter(new SimpleDialogAdapter(context,
                R.layout.dialog_context_list_row, items));

        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                dismiss();
                if (position == 0) {
                    confirmDelete.show(event);
                }
            }
        });
    }

    public void deleteEvent(Events event) {
        if (act != null)
            act.deleteEvent(event);
    }

    public void show(Events event) {
        this.event = event;
        titleTV.setText(R.string.options);

        show();
    }

    @Override
    public void show() {
        super.show();
        String s = titleTV.getText().toString();

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;

        int orientation = getContext().getResources().getConfiguration().orientation;
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(getWindow().getAttributes());

        if(orientation == ImageHelper.LANDSCAPE){
            lp.width = width / 2;
        } else if (orientation == ImageHelper.PORTRAIT)
            lp.width = width - 100;

        lp.x = 0;
        lp.y = 0;
        getWindow().setAttributes(lp);
    }
}