package com.minano.themovielibrary.views.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.minano.themovielibrary.R;
import com.minano.themovielibrary.controllers.TVSHOWItemCallerResponse;
import com.minano.themovielibrary.controllers.TVShowItemCaller;
import com.minano.themovielibrary.helpers.ImageDownloader;
import com.minano.themovielibrary.helpers.TVShowJSONParser;
import com.minano.themovielibrary.models.TVShowItem;

/**
 * Created by Admin on 17/09/2016.
 */
public class TVShowFragment extends Fragment {
    public static final String ARG_ID = "arg_id";
    private static final String BASE_URL = "http://api.themoviedb.org/3/tv/";
    private static final String API_KEY = "?api_key=5300b939a689dbb7cc14fae0dc554a9e";

    private TVShowItemCaller caller;
    private ImageDownloader imageDownloader;
    private TextView title;
    private TextView genre;
    private TextView firstAirDate;
    private TextView score;
    private TextView votes;
    private TextView description;
    private TextView productCompanies;
    private TextView status;
    private TextView episodeRuntime;
    private TextView revenue;
    private TextView seasons;
    private TextView episodes;
    private ImageView image;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_item_tvshow, container, false);

        title = (TextView) view.findViewById(R.id.original_title);
        firstAirDate = (TextView) view.findViewById(R.id.first_air_date);
        genre = (TextView) view.findViewById(R.id.genre);
        score = (TextView) view.findViewById(R.id.score);
        votes = (TextView) view.findViewById(R.id.vote_count);
        description = (TextView) view.findViewById(R.id.description);
        productCompanies = (TextView) view.findViewById(R.id.production_companies);
        status = (TextView) view.findViewById(R.id.status);
        episodeRuntime = (TextView) view.findViewById(R.id.runtime);
        seasons = (TextView) view.findViewById(R.id.seasons);
        episodes = (TextView) view.findViewById(R.id.episodes);
        image = (ImageView) view.findViewById(R.id.front_image);

        Bundle args = getArguments();
        if (args != null) {
            int id = args.getInt(ARG_ID);
            showItem(id, view);
        } else {
//            ponInfoLibro(0, vista);
        }

        return view;
    }

    public void showItem(int id) {
        showItem(id, getView());
    }

    private void showItem(int id, final View view) {
        caller = new TVShowItemCaller(BASE_URL + String.valueOf(id) + API_KEY, new TVShowJSONParser(), new TVSHOWItemCallerResponse() {
            @Override
            public void onCompleted(TVShowItem tvShowItem) {
                title.setText(tvShowItem.getTitle());
                genre.setText(tvShowItem.getGenre());
                firstAirDate.setText(tvShowItem.getFirstAirDate());
                score.setText(String.valueOf(tvShowItem.getPoints()));
                votes.setText(String.valueOf(tvShowItem.getVotes()));
                description.setText(tvShowItem.getOverview());
                productCompanies.setText(tvShowItem.getProductionCompanies());
                status.setText(tvShowItem.getStatus());
                episodeRuntime.setText(String.valueOf(tvShowItem.getEpisodeRuntime())+" Min.");
                episodes.setText(String.valueOf(tvShowItem.getNumberOfEpisodes()));
                seasons.setText(String.valueOf(tvShowItem.getNumberOfSeasons()));
                imageDownloader = new ImageDownloader("http://image.tmdb.org/t/p/w500"+tvShowItem.getImageUrl(), image);
                imageDownloader.execute();
            }
        });
        caller.execute();

    }
}
