package com.example.providers;

import java.util.Calendar;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainListFragment extends ListFragment {

  String[] items;
  View detailsFrame;
  boolean twoPanes = false;
  String item_alarm_clock;
  String item_browser_data;
  String item_call_log;
  String item_images;
  String item_settings;
  String item_contacts;
  Fragment fragBrowser = null;
  Fragment fragCallLog = null;
  Fragment fragContacts = null;
  Fragment fragImages = null;
  Fragment fragSettings = null;

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    item_alarm_clock = getResources().getString(R.string.item_alarm_clock);
    item_browser_data = getResources().getString(R.string.item_browser_data);
    item_call_log = getResources().getString(R.string.item_call_log);
    item_images = getResources().getString(R.string.item_images);
    item_settings = getResources().getString(R.string.item_settings);
    item_contacts = getResources().getString(R.string.item_contacts);
    items = getResources().getStringArray(R.array.providers);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, items);
    setListAdapter(aa);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    twoPanes = false;
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      twoPanes = true;
    }
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    if (twoPanes) {
      FragmentManager fm = getFragmentManager();
      Fragment fragment = null;
      getListView().setItemChecked(position, true);
      if (items[position].equals(item_images)) {
        if (fragImages == null)
          fragImages = new ImagePagerActivity.ImagePagerFragment();
        if (fragImages != null) {
          FragmentTransaction ft = fm.beginTransaction();
          ft.replace(R.id.details, fragImages);
          ft.commit();
        }
      }
      else {
        if (items[position].equals(item_alarm_clock)) {
          setAlarm();
        }
        else if (items[position].equals(item_browser_data)) {
          if (fragBrowser == null)
            fragBrowser = new BrowserDataActivity.BrowserDataListFragment();
          fragment = fragBrowser;
        }
        else if (items[position].equals(item_call_log)) {
          if (fragCallLog == null)
            fragCallLog = new CallLogActivity.CallLogListFragment();
          fragment = fragCallLog;
        }
        else if (items[position].equals(item_settings)) {
          if (fragSettings == null)
            fragSettings = new SettingsActivity.SettingsListFragment();
          fragment = fragSettings;
        }
        else if (items[position].equals(item_contacts)) {
          if (fragContacts == null)
            fragContacts = new ContactsActivity.ContactsListFragment();
          fragment = fragContacts;
        }
        if (fragment != null) {
          FragmentTransaction ft = fm.beginTransaction();
          ft.replace(R.id.details, fragment);
          ft.commit();
        }
      }
    }
    else {
      Intent intent = null;
      if (items[position].equals(item_alarm_clock)) {
        setAlarm();
      }
      else if (items[position].equals(item_browser_data)) {
        intent = new Intent("com.example.providers.BROWSER");
      }
      else if (items[position].equals(item_call_log)) {
        intent = new Intent("com.example.providers.CALL_HISTORY");
      }
      else if (items[position].equals(item_images)) {
        intent = new Intent("com.example.providers.IMAGE_GALLERY");
      }
      else if (items[position].equals(item_settings)) {
        intent = new Intent("com.example.providers.SETTINGS");
      }
      else if (items[position].equals(item_contacts)) {
        intent = new Intent("com.example.providers.CONTACTS");
      }
      if (intent != null) {
        startActivity(intent);
      }
    }
  }

  private void setAlarm() {
    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minutes = cal.get(Calendar.MINUTE);
    if (minutes <= 55) {
      minutes += 5;
    }
    else {
      minutes = 60 - minutes;
      if (hour < 23) {
        hour++;
      }
      else {
        hour = 0;
      }
    }
    intent = new Intent(AlarmClock.ACTION_SET_ALARM);
    intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
    intent.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
    startActivity(intent);
    Toast.makeText(getActivity(), "Alarm set for " + hour + ":" + minutes,
        Toast.LENGTH_LONG).show();
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.main, menu);
  }

}
