/*
 * Copyright 2016.  Julia Kozhukhovskaya
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.julia.popularmovies.details;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.julia.popularmovies.Config;
import com.example.julia.popularmovies.R;
import com.example.julia.popularmovies.models.Trailer;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

class TrailerListAdapter extends RecyclerView.Adapter<TrailerListAdapter.ViewHolder>  {

    private ArrayList<Trailer> mTrailers;
    private Context mContext;

    TrailerListAdapter(ArrayList<Trailer> trailers) {
        mTrailers = trailers;
    }

    public void add(Trailer trailer) {
        mTrailers.add(trailer);
        notifyDataSetChanged();
    }

    public void add(ArrayList<Trailer> trailers) {
        mTrailers.clear();
        mTrailers.addAll(trailers);
        notifyDataSetChanged();
    }

    ArrayList<Trailer> getTrailers() {
        return mTrailers;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_movie_trailer, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Trailer trailer = mTrailers.get(position);
        Picasso.with(mContext)
                .load(Config.TMD_TRAILER_YOUTUBE_URL + trailer.getKey() + Config.YOUTUBE_IMAGE_PATH)
                .into(holder.imageView);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(trailer.getTrailerUrl()));
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    class ViewHolder extends RecyclerView.ViewHolder  {
        final ImageView imageView;

        ViewHolder(View view) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.trailer_image);
        }
    }
}
@@ -2589,16 +2589,751 @@
 rl()));%0A
+                /**%0A                 * If there are no apps on the device that can receive the implicit intent,%0A                 * your app will crash when it calls startActivity(). To first verify that%0A                 * an app exists to receive the intent, call resolveActivity() on your Intent object.%0A                 * If the result is non-null, there is at least one app that can handle the intent%0A                 * and it's safe to call startActivity(). If the result is null,%0A                 * you should not use the intent and, if possible,%0A                 * you should disable the feature that invokes the intent.%0A                 */%0A                if (intent.resolveActivity(mContext.getPackageManager()) != null) %7B%0A    
         
@@ -3372,16 +3372,34 @@
 ntent);%0A
+                %7D%0A
         

Add Runtime Check to start an activity if there is no web browsers for watching trailers