package com.minano.themovielibrary.views;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.minano.themovielibrary.controllers.ItemCaller;
import com.minano.themovielibrary.controllers.ItemCallerResponse;
import com.minano.themovielibrary.helpers.FilmDetailParser;
import com.minano.themovielibrary.helpers.ImageDownloader;
import com.minano.themovielibrary.R;
import com.minano.themovielibrary.helpers.LiveStreamParser;
import com.minano.themovielibrary.helpers.TVShowDetailParser;
import com.minano.themovielibrary.models.DetailItem;

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

/**
 * Created by Admin on 31/08/2016.
 */
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

    final public static String TAG = "ItemAdapter";

    public static final int TOPRATED_FILMS = 1;
    public static final int MOSTPOPULAR_FILMS = 2;
    public static final int TOPRATED_TVSHOWS = 4;
    public static final int MOSTPOPULAR_TVSHOWS = 5;
    public static final int SEARCH_FILMS = 6;
    public static final int SEARCH_TV_SHOWS = 7;
    public static final int LIVE_STREAMS = 8;

    private static final String API_KEY = "&api_key=5300b939a689dbb7cc14fae0dc554a9e";
    private static final String TOPRATED_FILMS_URL = "http://api.themoviedb.org/3/movie/top_rated?page=";
    private static final String MOSTPOPULAR_FILMS_URL = "http://api.themoviedb.org/3/movie/popular?page=";
    private static final String TOPRATED_TVSHOWS_URL = "http://api.themoviedb.org/3/tv/top_rated?page=";
    private static final String MOSTPOPULAR_TVSHOWS_URL = "http://api.themoviedb.org/3/tv/popular?page=";
    private static final String SEARCH_FILMS_URL = "http://api.themoviedb.org/3/search/movie?api_key=5300b939a689dbb7cc14fae0dc554a9e&page=";
    private static final String SEARCH_TVSHOWS_URL = "http://api.themoviedb.org/3/search/tv?api_key=5300b939a689dbb7cc14fae0dc554a9e&page=";
    private final static String BASE_IMAGE_URL = "http://image.tmdb.org/t/p/w500";

    private static final String LIVE_STREAMS_URL = "http://pastebin.com/raw/Qz2G3inY";


    private int type = TOPRATED_FILMS;
    private LayoutInflater inflator;
    private List<DetailItem> detailItemList;
    private View.OnClickListener onClickListener;
    private ImageDownloader imageDownloader;
    private int page = 1;
    private String nameItemToSearch;

    public ItemAdapter(Context context) {
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        detailItemList = new ArrayList<DetailItem>();
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setNameItemToSearch(String name) {
        nameItemToSearch = name;
//        recalculateList();
    }

    public DetailItem getDetailItem(int position) {
        return detailItemList.get(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflator.inflate(R.layout.selector_item, null);
        v.setOnClickListener(onClickListener);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DetailItem detailItem = detailItemList.get(position);
        if (type != LIVE_STREAMS) {
            Log.d(TAG, detailItem.toString());
            holder.title.setText(detailItem.getTitle());
            holder.releaseDate.setText(detailItem.getReleaseDate());
            holder.score.setText(String.valueOf(detailItem.getPoints()));
            holder.votes.setText(String.valueOf(detailItem.getVotes()));
            imageDownloader = new ImageDownloader(BASE_IMAGE_URL + detailItem.getImageString(), holder.image);
            imageDownloader.execute();
        } else {
            Log.d(TAG, "PASA POR Type==LIVE_STREAMS");
            holder.title.setText(detailItem.getTitle());
            holder.releaseDate.setVisibility(View.INVISIBLE);
            holder.score.setVisibility(View.INVISIBLE);
            holder.votes.setVisibility(View.INVISIBLE);
            imageDownloader = new ImageDownloader(detailItem.getImageString(), holder.image);
            imageDownloader.execute();
        }
    }

    @Override
    public int getItemCount() {
        return detailItemList.size();
    }

    public void setOnItemClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setItemType(int type) {
        this.type = type;
        recalculateList();
    }

    public void recalculateList() {
        Log.d(TAG, String.valueOf(page));
        ItemCaller itemCaller = null;
        switch (type) {
            case TOPRATED_FILMS:
                itemCaller = new ItemCaller(TOPRATED_FILMS_URL + String.valueOf(page) + API_KEY, new FilmDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
//                        notifyDataSetChanged();
                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case MOSTPOPULAR_FILMS:
                itemCaller = new ItemCaller(MOSTPOPULAR_FILMS_URL + String.valueOf(page) + API_KEY, new FilmDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
//                        notifyDataSetChanged();
                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case TOPRATED_TVSHOWS:
                itemCaller = new ItemCaller(MOSTPOPULAR_TVSHOWS_URL + String.valueOf(page) + API_KEY, new TVShowDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
//                        notifyDataSetChanged();
                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case MOSTPOPULAR_TVSHOWS:
                itemCaller = new ItemCaller(TOPRATED_TVSHOWS_URL + String.valueOf(page) + API_KEY, new TVShowDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
//                        notifyDataSetChanged();
                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case LIVE_STREAMS:
                itemCaller = new ItemCaller(LIVE_STREAMS_URL, new LiveStreamParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
//                        if (page == 1)
                        detailItemList = itemList;
//                        else
//                            detailItemList.addAll(itemList);
                        notifyDataSetChanged();
//                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case SEARCH_FILMS:
                itemCaller = new ItemCaller(SEARCH_FILMS_URL + String.valueOf(page) + "&query=" +  nameItemToSearch.replace(" ", "%20"), new FilmDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
                        notifyDataSetChanged();
//                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
            case SEARCH_TV_SHOWS:
                itemCaller = new ItemCaller(SEARCH_TVSHOWS_URL + String.valueOf(page) + "&query=" +  nameItemToSearch.replace(" ", "%20"), new TVShowDetailParser(), new ItemCallerResponse() {
                    @Override
                    public void onCompleted(List<DetailItem> itemList) {
                        if (page == 1)
                            detailItemList = itemList;
                        else
                            detailItemList.addAll(itemList);
                        notifyDataSetChanged();
//                        notifyItemRangeChanged(0, getItemCount());
//                        notifyItemRangeInserted(0 , detailItemList.size()-1);
                    }
                });
                break;
        }
        itemCaller.execute();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView title;
        public TextView releaseDate;
        public TextView votes;
        public TextView score;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.title);
            releaseDate = (TextView) itemView.findViewById(R.id.release_date);
            votes = (TextView) itemView.findViewById(R.id.vote_count);
            score = (TextView) itemView.findViewById(R.id.score);
        }
    }
}
