package org.metasyntactic;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import org.metasyntactic.data.Movie;
import org.metasyntactic.data.Performance;
import org.metasyntactic.data.Theater;

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

public class TheaterDetailsActivity extends ListActivity {
  /** Called when the activity is first created. */
  private Theater theater;
  private List<Movie> movies = new ArrayList<Movie>();

  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NowPlayingControllerWrapper.removeActivity(this);
    setContentView(R.layout.theaterdetails);
    this.theater = getIntent().getExtras().getParcelable("theater");
    bindView();
  }

  @Override
  protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
    final Movie movie = this.movies.get(position);
    final Intent intent = new Intent();
    intent.setClass(this, MovieDetailsActivity.class);
    intent.putExtra("movie", (Parcelable) movie);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override
  protected void onDestroy() {
    NowPlayingControllerWrapper.removeActivity(this);
    super.onDestroy();
  }

  private void bindView() {
    final TextView theaterLabel = (TextView) findViewById(R.id.theater);
    theaterLabel.setEllipsize(TextUtils.TruncateAt.END);
    theaterLabel.setText(this.theater.getName());

    final TextView phoneLabel = (TextView) findViewById(R.id.phone);
    phoneLabel.setEllipsize(TextUtils.TruncateAt.END);
    phoneLabel.setText(this.theater.getPhoneNumber());

    final TextView mapLabel = (TextView) findViewById(R.id.map);
    mapLabel.setEllipsize(TextUtils.TruncateAt.END);
    final String address = this.theater.getAddress() + ", " + this.theater.getLocation().getCity();
    mapLabel.setText(address);

    this.movies = NowPlayingControllerWrapper.getMoviesAtTheater(this.theater);
    final ImageView mapIcon = (ImageView) findViewById(R.id.mapicon);
    final ImageView phoneIcon = (ImageView) findViewById(R.id.phoneicon);
    final Intent mapIntent = new Intent("android.intent.action.VIEW", Uri
        .parse("geo:0,0?q=" + address));
    final Intent callIntent = new Intent("android.intent.action.DIAL", Uri
        .parse("tel:" + this.theater.getPhoneNumber()));

    mapIcon.setOnClickListener(new OnClickListener() {
      public void onClick(final View arg0) {
        startActivity(mapIntent);
      }
    });

    mapLabel.setOnClickListener(new OnClickListener() {
      public void onClick(final View arg0) {
        startActivity(mapIntent);
      }
    });

    phoneIcon.setOnClickListener(new OnClickListener() {
      public void onClick(final View arg0) {
        startActivity(callIntent);
      }
    });

    phoneLabel.setOnClickListener(new OnClickListener() {
      public void onClick(final View arg0) {
        startActivity(callIntent);
      }
    });

    MoviesAdapter moviesAdapter = new MoviesAdapter();
    setListAdapter(moviesAdapter);
  }

  private class MoviesAdapter extends BaseAdapter {
    private final LayoutInflater inflater;

    public MoviesAdapter() {
      // Cache the LayoutInflate to avoid asking for a new one each time.
      this.inflater = LayoutInflater.from(TheaterDetailsActivity.this);
    }

    public Object getEntry(final int i) {
      return i;
    }

    public View getView(final int position, View convertView, final ViewGroup viewGroup) {
      convertView = this.inflater.inflate(R.layout.theaterdetails_item, null);
      final MovieViewHolder holder = new MovieViewHolder();
      holder.label = (TextView) convertView.findViewById(R.id.label);
      holder.data = (TextView) convertView.findViewById(R.id.data);
      final Movie movie = TheaterDetailsActivity.this.movies.get(position);
      holder.label.setText(movie.getDisplayTitle());
      final List<Performance> list = NowPlayingControllerWrapper
          .getPerformancesForMovieAtTheater(movie, TheaterDetailsActivity.this.theater);
      String performance = "";
      if (list != null) {
        for (Performance aList : list) {
          performance += aList.getTime() + ", ";
        }
        performance = performance
            .substring(0, performance.length() - 2);
        holder.data.setText(performance);
      } else {
        holder.data.setText("Unknown.");
      }
      return convertView;
    }

    public int getCount() {
      return TheaterDetailsActivity.this.movies.size();
    }

    private class MovieViewHolder {
      private TextView label;
      private TextView data;
    }

    public long getEntryId(final int position) {
      return position;
    }

    public Object getItem(final int position) {
      return TheaterDetailsActivity.this.movies.get(position);
    }

    public long getItemId(final int position) {
      return position;
    }

    public void refresh() {
      notifyDataSetChanged();
    }
  }
}

@@ -4364,27 +4364,16 @@
 rWrapper
-%0A          
 .getPerf
@@ -4404,16 +4404,114 @@
 r(movie,
+%0A                                                                                                 
  Theater
@@ -4576,36 +4576,8 @@
 %22%22;%0A
-      if (list != null) %7B%0A  
     
@@ -4607,26 +4607,24 @@
 t : list) %7B%0A
-  
         perf
@@ -4660,30 +4660,26 @@
 %22, %22;%0A      
-  %7D%0A  
+%7D%0A
       perfor
@@ -4697,29 +4697,16 @@
 formance
-%0A            
 .substri
@@ -4740,26 +4740,24 @@
 - 2);%0A      
-  
 holder.data.
@@ -4782,71 +4782,8 @@
 e);%0A
-      %7D else %7B%0A        holder.data.setText(%22Unknown.%22);%0A      %7D
 %0A   

list can't be null