package com.github.skittlesdev.kubrick;

import android.app.Activity;
import android.graphics.Movie;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;
import com.github.skittlesdev.kubrick.asyncs.GetMovieTask;
import com.github.skittlesdev.kubrick.asyncs.GetSeriesTask;
import com.github.skittlesdev.kubrick.interfaces.MediaListener;
import com.github.skittlesdev.kubrick.ui.menus.DrawerMenu;
import com.github.skittlesdev.kubrick.ui.menus.ToolbarMenu;
import com.squareup.picasso.Picasso;
import info.movito.themoviedbapi.model.MovieDb;
import info.movito.themoviedbapi.model.core.IdElement;
import info.movito.themoviedbapi.model.tv.TvSeries;
import org.joda.time.Duration;
import org.joda.time.format.*;

public class MediaActivity extends Activity implements MediaListener {
    private boolean isTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_movie);

        this.setActionBar((Toolbar) this.findViewById(R.id.toolBar));
        getActionBar().setDisplayHomeAsUpEnabled(true);

        new DrawerMenu(this, (DrawerLayout) findViewById(R.id.homeDrawerLayout), (RecyclerView) findViewById(R.id.homeRecyclerView)).draw();

        int mediaId = this.getIntent().getIntExtra("MEDIA_ID", -1);

        if (this.getIntent().getStringExtra("MEDIA_TYPE").compareTo("tv") == 0) {
            this.isTv = true;
            GetSeriesTask task = new GetSeriesTask(this);
            task.execute(mediaId);
        }
        else {
            this.isTv = false;
            GetMovieTask task = new GetMovieTask(this);
            task.execute(mediaId);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        new ToolbarMenu(this).itemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    private void showPoster(IdElement media) {
        String posterPath;

        if (!this.isTv) {
            posterPath = ((MovieDb) media).getPosterPath();
        }
        else {
            posterPath = ((TvSeries) media).getPosterPath();
        }

        Picasso.with(getApplicationContext())
                .load("http://image.tmdb.org/t/p/w500" + posterPath)
                .placeholder(R.drawable.poster_default_placeholder)
                .error(R.drawable.poster_default_error)
                .into((ImageView) findViewById(R.id.poster));
    }

    private void showTitle(IdElement media) {
        TextView titleView = (TextView) findViewById(R.id.title);
        if (!this.isTv) {
            String title = ((MovieDb) media).getTitle();
            String year = "(" + ((MovieDb) media).getReleaseDate().split("-")[0] + ")";
            titleView.setText(title + " " + year);
        }
        else {
            String title = ((TvSeries) media).getName();
            String firstYear = ((TvSeries) media).getFirstAirDate().split("-")[0];
            String lastYear = ((TvSeries) media).getLastAirDate().split("-")[0];
            titleView.setText(title + " (" + firstYear + " - " + lastYear + ")");
        }
    }

    private void showDuration(MovieDb movie) {
        TextView durationView = (TextView) findViewById(R.id.duration);

        Duration duration = new Duration(movie.getRuntime() * 1000 * 60);
        PeriodFormatter formatter = new PeriodFormatterBuilder()
                .appendHours()
                .appendSuffix(":")
                .minimumPrintedDigits(2)
                .appendMinutes()
                .toFormatter();

        String durationDisplay = formatter.print(duration.toPeriod());

        durationView.setText(durationDisplay);
    }

    private void showOverview(IdElement media) {
        TextView overviewView = (TextView) findViewById(R.id.overview);

        String overview;
        if (!this.isTv) {
            overview = ((MovieDb) media).getOverview();
        }
        else {
            overview = ((TvSeries) media).getOverview();
        }

        overviewView.setText(overview);
    }

    @Override
    public void onMediaRetrieved(IdElement media) {
        showPoster(media);
        showTitle(media);

        if (!this.isTv) {
            showDuration((MovieDb) media);
        }

        showOverview(media);
    }
}

@@ -1032,35 +1032,8 @@
 r %7B%0A
-    private boolean isTv;%0A%0A
     
@@ -1038,24 +1038,24 @@
   @Override%0A
+
     protecte
@@ -1622,38 +1622,8 @@
 ) %7B%0A
-            this.isTv = true;%0A
     
@@ -1740,39 +1740,8 @@
 e %7B%0A
-            this.isTv = false;%0A
     
@@ -2302,34 +2302,48 @@
         if (
-!this.isTv
+media instanceof MovieDb
 ) %7B%0A        
@@ -2919,34 +2919,48 @@
         if (
-!this.isTv
+media instanceof MovieDb
 ) %7B%0A        
@@ -4205,34 +4205,48 @@
         if (
-!this.isTv
+media instanceof MovieDb
 ) %7B%0A        
@@ -4569,18 +4569,32 @@
 if (
-!this.isTv
+media instanceof MovieDb
 ) %7B%0A

Drop isTv boolean in MediaActivity for instanceof tests