package com.example.android.popularmovies;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * A placeholder fragment containing a simple view.
 */
public class DetailFragment extends Fragment {

    private static final String LOG_TAG = DetailFragment.class.getSimpleName();
    private Movie movie;
    private TmdbAccess tmdbAccess;

    private ImageView posterImage;
    private TextView title;
    private TextView overview;
    private TextView voteAverage;
    private TextView releaseDate;
    private TextView duration;

    private BroadcastReceiver receiver;

    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
        tmdbAccess=new TmdbAccess();
        Intent intent = getActivity().getIntent();
        if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
            movie =  intent.getParcelableExtra(Intent.EXTRA_TEXT);
            title = (TextView) rootView.findViewById(R.id.title);
            posterImage = (ImageView) rootView.findViewById(R.id.posterImage);
            overview = (TextView) rootView.findViewById(R.id.overview);
            voteAverage = (TextView) rootView.findViewById(R.id.voteAverage);
            releaseDate = (TextView) rootView.findViewById(R.id.releaseYear);
            duration = (TextView) rootView.findViewById(R.id.duration);
        }
        return rootView;
    }


    @Override
    public void onStart() {
        receiver = new InternetReceiver(getActivity()) {
            @Override
            protected void refresh() {
                FetchOneMovieTask movieTask = new FetchOneMovieTask();
                movieTask.execute(movie);
            }
        };

        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(receiver, filter);
        super.onStart();
    }

    @Override
    public void onStop() {
        getActivity().unregisterReceiver(receiver);
        super.onStop();
    }

    public class FetchOneMovieTask extends AsyncTask<Movie, Void, Movie> {


        @Override
        protected Movie doInBackground(Movie... params) {
            return params[0];
        }


        @Override
        protected void onPostExecute(Movie result) {
            if (result != null) {
                title.setText(result.getTitle());
                overview.setText(result.getOverview());
                voteAverage.setText(result.getVoteAverage()+getString(R.string.rateMax));
                releaseDate.setText(result.getReleaseDate().split("-")[0]); //To extract the year from the date
                duration.setText(result.getDuration()+" "+getString(R.string.min));
                Picasso.with(getActivity()).load(tmdbAccess.constructPosterImageURL(result.getPosterName()).toString()).into(posterImage);
            }
        }
    }
}
@@ -191,37 +191,8 @@
 er;%0A
-import android.os.AsyncTask;%0A
 impo
@@ -213,16 +213,16 @@
 Bundle;%0A
+
 import a
@@ -562,88 +562,8 @@
  %7B%0A%0A
-    private static final String LOG_TAG = DetailFragment.class.getSimpleName();%0A
     
@@ -1791,16 +1791,388 @@
 ration);
+%0A%0A            title.setText(movie.getTitle());%0A            overview.setText(movie.getOverview());%0A            voteAverage.setText(movie.getVoteAverage()+getString(R.string.rateMax));%0A            releaseDate.setText(movie.getReleaseDate().split(%22-%22)%5B0%5D); //To extract the year from the date%0A            duration.setText(movie.getDuration() + %22 %22 + getString(R.string.min));
 %0A       
@@ -2387,101 +2387,126 @@
     
-FetchOneMovieTask movieTask = new FetchOneMovieTask();%0A                movieTask.execute(movi
+Picasso.with(getActivity()).load(tmdbAccess.constructPosterImageURL(movie.getPosterName()).toString()).into(posterImag
 e);%0A
@@ -2838,866 +2838,5 @@
  %7D%0A%0A
-    public class FetchOneMovieTask extends AsyncTask%3CMovie, Void, Movie%3E %7B%0A%0A%0A        @Override%0A        protected Movie doInBackground(Movie... params) %7B%0A            return params%5B0%5D;%0A        %7D%0A%0A%0A        @Override%0A        protected void onPostExecute(Movie result) %7B%0A            if (result != null) %7B%0A                title.setText(result.getTitle());%0A                overview.setText(result.getOverview());%0A                voteAverage.setText(result.getVoteAverage()+getString(R.string.rateMax));%0A                releaseDate.setText(result.getReleaseDate().split(%22-%22)%5B0%5D); //To extract the year from the date%0A                duration.setText(result.getDuration()+%22 %22+getString(R.string.min));%0A                Picasso.with(getActivity()).load(tmdbAccess.constructPosterImageURL(result.getPosterName()).toString()).into(posterImage);%0A            %7D%0A        %7D%0A    %7D%0A
 %7D

Remove detail fragment tmdb api call