/**
     * Format data for the application
     *
     * @param existingList List to be extended
     * @return List with items
     */
    public ArrayList<Media> formatForPopcorn(ArrayList<Media> existingList) {
      ArrayList<LinkedTreeMap<String, Object>> movies = new ArrayList<>();
      if (data != null) {
        movies = (ArrayList<LinkedTreeMap<String, Object>>) data.get("movies");
      }

      for (LinkedTreeMap<String, Object> item : movies) {
        Movie movie = new Movie(sMediaProvider, sSubsProvider);

        Double id = (Double) item.get("id");
        movie.videoId = Integer.toString(id.intValue());
        movie.imdbId = (String) item.get("imdb_code");

        int existingItem = isInResults(existingList, movie.videoId);
        if (existingItem == -1) {
          movie.title = (String) item.get("title");
          Double year = (Double) item.get("year");
          movie.year = Integer.toString(year.intValue());
          movie.rating = item.get("rating").toString();
          movie.genre = ((ArrayList<String>) item.get("genres")).get(0);
          movie.image = (String) item.get("medium_cover_image");

          if (movie.image != null) {
            movie.image = movie.image.replace("medium-cover", "large-cover");
          }

          ArrayList<LinkedTreeMap<String, Object>> torrents =
              (ArrayList<LinkedTreeMap<String, Object>>) item.get("torrents");
          if (torrents != null) {
            for (LinkedTreeMap<String, Object> torrentObj : torrents) {
              String quality = (String) torrentObj.get("quality");
              if (quality == null || quality.equals("3D")) continue;

              Media.Torrent torrent = new Media.Torrent();

              torrent.seeds = ((Double) torrentObj.get("seeds")).intValue();
              torrent.peers = ((Double) torrentObj.get("peers")).intValue();
              torrent.hash = (String) torrentObj.get("hash");
              try {
                torrent.url =
                    "magnet:?xt=urn:btih:"
                        + torrent.hash
                        + "&amp;dn="
                        + URLEncoder.encode(item.get("title_long").toString(), "utf-8")
                        + "&amp;tr=http://exodus.desync.com:6969/announce&amp;tr=udp://tracker.openbittorrent.com:80/announce&amp;tr=udp://open.demonii.com:1337/announce&amp;tr=udp://exodus.desync.com:6969/announce&amp;tr=udp://tracker.yify-torrents.com/announce";
              } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                torrent.url = (String) torrentObj.get("url");
              }

              movie.torrents.put(quality, torrent);
            }
          }

          existingList.add(movie);
        }
      }
      return existingList;
    }
    /**
     * Format data for the application
     *
     * @return List with items
     */
    public Movie formatDetailForPopcorn() {
      Movie movie = new Movie(sMediaProvider, sSubsProvider);
      LinkedTreeMap<String, Object> movieObj = data;
      if (movieObj == null) return movie;

      Double id = (Double) movieObj.get("id");
      movie.videoId = Integer.toString(id.intValue());
      movie.imdbId = (String) movieObj.get("imdb_code");

      movie.title = (String) movieObj.get("title");
      Double year = (Double) movieObj.get("year");
      movie.year = Integer.toString(year.intValue());
      movie.rating = movieObj.get("rating").toString();
      movie.genre = ((ArrayList<String>) movieObj.get("genres")).get(0);

      ArrayList<LinkedTreeMap<String, Object>> torrents =
          (ArrayList<LinkedTreeMap<String, Object>>) movieObj.get("torrents");
      if (torrents != null) {
        for (LinkedTreeMap<String, Object> torrentObj : torrents) {
          String quality = (String) torrentObj.get("quality");
          if (quality == null) continue;

          Media.Torrent torrent = new Media.Torrent();

          torrent.seeds = ((Double) torrentObj.get("seeds")).intValue();
          torrent.peers = ((Double) torrentObj.get("peers")).intValue();
          torrent.hash = (String) torrentObj.get("hash");
          try {
            String magnet =
                "magnet:?xt=urn:btih:"
                    + torrent.hash
                    + "&amp;dn="
                    + URLEncoder.encode(movieObj.get("title_long").toString(), "utf-8")
                    + "&amp;tr=http://exodus.desync.com:6969/announce&amp;tr=udp://tracker.openbittorrent.com:80/announce&amp;tr=udp://open.demonii.com:1337/announce&amp;tr=udp://exodus.desync.com:6969/announce&amp;tr=udp://tracker.yify-torrents.com/announce";
            torrent.url = magnet;
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            torrent.url = (String) torrentObj.get("url");
          }

          movie.torrents.put(quality, torrent);
        }
      }

      return movie;
    }
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  @Override
  public View onCreateView(
      LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.fragment_moviedetail, container, false);
    if (VersionUtils.isJellyBean() && container != null) {
      mRoot.setMinimumHeight(container.getMinimumHeight());
    }
    ButterKnife.inject(this, mRoot);

    if (!VersionUtils.isJellyBean()) {
      mPlayButton.setBackgroundDrawable(
          PixelUtils.changeDrawableColor(
              mPlayButton.getContext(), R.drawable.play_button_circle, sMovie.color));
    } else {
      mPlayButton.setBackground(
          PixelUtils.changeDrawableColor(
              mPlayButton.getContext(), R.drawable.play_button_circle, sMovie.color));
    }

    mTitle.setText(sMovie.title);
    if (!sMovie.rating.equals("-1")) {
      Double rating = Double.parseDouble(sMovie.rating);
      mRating.setProgress(rating.intValue());
      mRating.setVisibility(View.VISIBLE);
    } else {
      mRating.setVisibility(View.GONE);
    }

    String metaDataStr = sMovie.year;
    if (!TextUtils.isEmpty(sMovie.runtime)) {
      metaDataStr += " • ";
      metaDataStr += sMovie.runtime + " " + getString(R.string.minutes);
    }

    if (!TextUtils.isEmpty(sMovie.genre)) {
      metaDataStr += " • ";
      metaDataStr += sMovie.genre;
    }

    mMeta.setText(metaDataStr);

    if (!TextUtils.isEmpty(sMovie.synopsis)) {
      mSynopsis.setText(sMovie.synopsis);
      mSynopsis.post(
          new Runnable() {
            @Override
            public void run() {
              boolean ellipsized = false;
              Layout layout = mSynopsis.getLayout();
              if (layout == null) return;
              int lines = layout.getLineCount();
              if (lines > 0) {
                int ellipsisCount = layout.getEllipsisCount(lines - 1);
                if (ellipsisCount > 0) {
                  ellipsized = true;
                }
              }
              mReadMore.setVisibility(ellipsized ? View.VISIBLE : View.GONE);
            }
          });
    } else {
      mSynopsis.setClickable(false);
      mReadMore.setVisibility(View.GONE);
    }

    mWatchTrailer.setVisibility(
        sMovie.trailer == null || sMovie.trailer.isEmpty() ? View.GONE : View.VISIBLE);

    mSubtitles.setFragmentManager(getFragmentManager());
    mQuality.setFragmentManager(getFragmentManager());
    mSubtitles.setTitle(R.string.subtitles);
    mQuality.setTitle(R.string.quality);

    mSubtitles.setText(R.string.loading_subs);
    mSubtitles.setClickable(false);

    if (sMovie.getSubsProvider() != null) {
      sMovie
          .getSubsProvider()
          .getList(
              sMovie,
              new SubsProvider.Callback() {
                @Override
                public void onSuccess(Map<String, String> subtitles) {
                  if (!mAttached) return;

                  if (subtitles == null) {
                    ThreadUtils.runOnUiThread(
                        new Runnable() {
                          @Override
                          public void run() {
                            mSubtitles.setText(R.string.no_subs_available);
                          }
                        });
                    return;
                  }

                  sMovie.subtitles = subtitles;

                  String[] languages = subtitles.keySet().toArray(new String[subtitles.size()]);
                  Arrays.sort(languages);
                  final String[] adapterLanguages = new String[languages.length + 1];
                  adapterLanguages[0] = "no-subs";
                  System.arraycopy(languages, 0, adapterLanguages, 1, languages.length);

                  String[] readableNames = new String[adapterLanguages.length];
                  for (int i = 0; i < readableNames.length; i++) {
                    String language = adapterLanguages[i];
                    if (language.equals("no-subs")) {
                      readableNames[i] = getString(R.string.no_subs);
                    } else {
                      Locale locale = LocaleUtils.toLocale(language);
                      readableNames[i] = locale.getDisplayName(locale);
                    }
                  }

                  mSubtitles.setListener(
                      new OptionSelector.SelectorListener() {
                        @Override
                        public void onSelectionChanged(int position, String value) {
                          onSubtitleLanguageSelected(adapterLanguages[position]);
                        }
                      });
                  mSubtitles.setData(readableNames);
                  ThreadUtils.runOnUiThread(
                      new Runnable() {
                        @Override
                        public void run() {
                          mSubtitles.setClickable(true);
                        }
                      });

                  String defaultSubtitle =
                      PrefUtils.get(mSubtitles.getContext(), Prefs.SUBTITLE_DEFAULT, null);
                  if (subtitles.containsKey(defaultSubtitle)) {
                    onSubtitleLanguageSelected(defaultSubtitle);
                    mSubtitles.setDefault(Arrays.asList(adapterLanguages).indexOf(defaultSubtitle));
                  } else {
                    onSubtitleLanguageSelected("no-subs");
                    mSubtitles.setDefault(Arrays.asList(adapterLanguages).indexOf("no-subs"));
                  }
                }

                @Override
                public void onFailure(Exception e) {
                  mSubtitles.setData(new String[0]);
                  mSubtitles.setClickable(true);
                }
              });
    } else {
      mSubtitles.setClickable(false);
      mSubtitles.setText(R.string.no_subs_available);
    }

    if (sMovie.torrents.size() > 0) {
      final String[] qualities =
          sMovie.torrents.keySet().toArray(new String[sMovie.torrents.size()]);
      SortUtils.sortQualities(qualities);
      mQuality.setData(qualities);
      mQuality.setListener(
          new OptionSelector.SelectorListener() {
            @Override
            public void onSelectionChanged(int position, String value) {
              mSelectedQuality = value;
              renderHealth();
              updateMagnet();
            }
          });
      String defaultQuality = PrefUtils.get(mQuality.getContext(), Prefs.QUALITY_DEFAULT, "720p");
      int qualityIndex =
          Arrays.asList(qualities).contains(defaultQuality)
              ? Arrays.asList(qualities).indexOf(defaultQuality)
              : qualities.length - 1;
      mSelectedQuality = qualities[qualityIndex];
      mQuality.setText(mSelectedQuality);
      mQuality.setDefault(qualityIndex);
      renderHealth();
      updateMagnet();
    }

    if (mCoverImage != null) {
      Picasso.with(mCoverImage.getContext()).load(sMovie.image).into(mCoverImage);
    }

    return mRoot;
  }