<commit_before>package com.dihanov.musiq.ui.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dihanov.musiq.R;
import com.dihanov.musiq.interfaces.MainViewFunctionable;
import com.dihanov.musiq.models.Album;
import com.dihanov.musiq.models.SpecificAlbum;
import com.dihanov.musiq.models.Tag;
import com.dihanov.musiq.models.Track;
import com.dihanov.musiq.service.LastFmApiClient;
import com.dihanov.musiq.ui.view_holders.AlbumViewHolder;
import com.dihanov.musiq.util.Constants;
import com.jakewharton.rxbinding2.view.RxView;
import com.veinhorn.tagview.TagView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by dimitar.dihanov on 11/2/2017.
 */

public class AlbumDetailsPopupWindow {
    private static final long DELAY_IN_MILLIS = 500;
    private static final String LOADING_ALBUM = "just a sec, let me find this album for you";

    private LastFmApiClient lastFmApiClient;
    private Activity activity;

    public AlbumDetailsPopupWindow(LastFmApiClient lastFmApiClient, Activity activity) {
        this.lastFmApiClient = lastFmApiClient;
        this.activity = activity;
    }

    public void showPopupWindow(MainViewFunctionable activity, AlbumViewHolder viewHolder, String artistName, String albumName, int mainWindowId) {
        final Album[] loadedAlbum = new Album[1];
        RxView.clicks(viewHolder.thumbnail)
                .debounce(DELAY_IN_MILLIS, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(click -> {
                    lastFmApiClient.getLastFmApiService()
                            .searchForSpecificAlbum(artistName, albumName)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .map(specificAlbum -> specificAlbum)
                            .subscribe(new Observer<SpecificAlbum>() {
                                @Override
                                public void onSubscribe(Disposable d) {
                                    activity.showProgressBar();
                                    Constants.showTooltip((Activity) activity, activity.getBirdIcon(), LOADING_ALBUM);
                                }

                                @Override
                                public void onNext(SpecificAlbum specificAlbum) {
                                    Album fullAlbum = specificAlbum.getAlbum();
                                    loadedAlbum[0] = fullAlbum;
                                }

                                @Override
                                public void onError(Throwable e) {
                                    Log.e(this.getClass().toString(), e.getMessage());
                                }

                                @Override
                                public void onComplete() {
                                    activity.hideProgressBar();
                                    showAlbumDetails((Activity) activity, loadedAlbum[0], mainWindowId);
                                }
                            });

                });
    }

    private void showAlbumDetails(Activity activity, Album album, int mainWindowId) {
        CoordinatorLayout mainLayout = (CoordinatorLayout) activity.findViewById(mainWindowId);

        LayoutInflater inflater = LayoutInflater.from(activity);
        View albumDetails = inflater.inflate(R.layout.album_popup_info, null);
        TextView tracks = albumDetails.findViewById(R.id.album_popup_tracks);
        TextView title = albumDetails.findViewById(R.id.album_popup_title);
        TextView wiki = albumDetails.findViewById(R.id.album_popup_wiki);
        ImageView cover = albumDetails.findViewById(R.id.album_popup_thumbnail);
        StringBuilder sb = new StringBuilder();
        for (Track track : album.getTracks().getTrack()) {
            long millis = Long.parseLong(track.getDuration());
            String duration = String.format("%02d:%02d",
                    TimeUnit.SECONDS.toMinutes(millis),
                    TimeUnit.SECONDS.toSeconds(millis) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(millis)));
            sb.append(String.format("%s - %s\n", track.getName(), duration));
        }

        initTags(album, albumDetails);
        tracks.setText(sb.toString());
        title.setText(album.getName());
        String modifiedBio = formatText(album.getWiki().getSummary());
        wiki.setText(Html.fromHtml(modifiedBio));
        wiki.setMovementMethod(LinkMovementMethod.getInstance());
        Glide.with(activity)
                .load(album.getImage().get(Constants.IMAGE_LARGE).getText())
                .into(cover);

        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true;
        final PopupWindow popupWindow = new PopupWindow(albumDetails, width, height, focusable);
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

        popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
    }


    @SuppressLint("ResourceType")
    private void initTags(Album album, View rootLayout) {
        TagView firstTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView secondTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView thirdTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView fourthTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView fifthTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);

        TagView[] tags = new TagView[]{
                firstTag, secondTag, thirdTag, fourthTag, fifthTag
        };


        List<Tag> tagText = album.getTags().getTag();

        if(tagText.isEmpty()){
            return;
        }

        //this is very ugly, however since there is no lambda what can you do
        List<String> firstFive = new ArrayList<String>() {
            {
                for (int i = 0; i < tagText.size(); i++) {
                    this.add(tagText.get(i).getName());
                }
            }<commit_msg>Fixed a very nasty crash relating to the wiki of the album turning out to be null.<commit_after>package com.dihanov.musiq.ui.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dihanov.musiq.R;
import com.dihanov.musiq.interfaces.MainViewFunctionable;
import com.dihanov.musiq.models.Album;
import com.dihanov.musiq.models.SpecificAlbum;
import com.dihanov.musiq.models.Tag;
import com.dihanov.musiq.models.Track;
import com.dihanov.musiq.service.LastFmApiClient;
import com.dihanov.musiq.ui.view_holders.AlbumViewHolder;
import com.dihanov.musiq.util.Constants;
import com.jakewharton.rxbinding2.view.RxView;
import com.veinhorn.tagview.TagView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by dimitar.dihanov on 11/2/2017.
 */

public class AlbumDetailsPopupWindow {
    private static final long DELAY_IN_MILLIS = 500;
    private static final String LOADING_ALBUM = "just a sec, let me find this album for you";

    private LastFmApiClient lastFmApiClient;
    private Activity activity;

    public AlbumDetailsPopupWindow(LastFmApiClient lastFmApiClient, Activity activity) {
        this.lastFmApiClient = lastFmApiClient;
        this.activity = activity;
    }

    public void showPopupWindow(MainViewFunctionable activity, AlbumViewHolder viewHolder, String artistName, String albumName, int mainWindowId) {
        final Album[] loadedAlbum = new Album[1];
        RxView.clicks(viewHolder.thumbnail)
                .debounce(DELAY_IN_MILLIS, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(click -> {
                    lastFmApiClient.getLastFmApiService()
                            .searchForSpecificAlbum(artistName, albumName)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .map(specificAlbum -> specificAlbum)
                            .subscribe(new Observer<SpecificAlbum>() {
                                @Override
                                public void onSubscribe(Disposable d) {
                                    activity.showProgressBar();
                                    Constants.showTooltip((Activity) activity, activity.getBirdIcon(), LOADING_ALBUM);
                                }

                                @Override
                                public void onNext(SpecificAlbum specificAlbum) {
                                    Album fullAlbum = specificAlbum.getAlbum();
                                    loadedAlbum[0] = fullAlbum;
                                }

                                @Override
                                public void onError(Throwable e) {
                                    Log.e(this.getClass().toString(), e.getMessage());
                                }

                                @Override
                                public void onComplete() {
                                    activity.hideProgressBar();
                                    showAlbumDetails((Activity) activity, loadedAlbum[0], mainWindowId);
                                }
                            });

                });
    }

    private void showAlbumDetails(Activity activity, Album album, int mainWindowId) {
        CoordinatorLayout mainLayout = (CoordinatorLayout) activity.findViewById(mainWindowId);

        LayoutInflater inflater = LayoutInflater.from(activity);
        View albumDetails = inflater.inflate(R.layout.album_popup_info, null);
        TextView tracks = albumDetails.findViewById(R.id.album_popup_tracks);
        TextView title = albumDetails.findViewById(R.id.album_popup_title);
        TextView wiki = albumDetails.findViewById(R.id.album_popup_wiki);
        ImageView cover = albumDetails.findViewById(R.id.album_popup_thumbnail);
        StringBuilder sb = new StringBuilder();
        for (Track track : album.getTracks().getTrack()) {
            long millis = Long.parseLong(track.getDuration());
            String duration = String.format("%02d:%02d",
                    TimeUnit.SECONDS.toMinutes(millis),
                    TimeUnit.SECONDS.toSeconds(millis) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(millis)));
            sb.append(String.format("%s - %s\n", track.getName(), duration));
        }

        initTags(album, albumDetails);
        tracks.setText(sb.toString());
        title.setText(album.getName());


        if(album.getWiki() != null){
            String toModify = album.getWiki().getSummary();
            String fullWiki = album.getWiki().getContent();
            String modifiedBio = "";
            if(toModify == null){
                toModify = fullWiki;
                if(toModify != null){
                    modifiedBio = formatText(toModify);
                }
            } else {
                modifiedBio = formatText(toModify);
            }

            wiki.setText(Html.fromHtml(modifiedBio));
            wiki.setMovementMethod(LinkMovementMethod.getInstance());
        }


        Glide.with(activity)
                .load(album.getImage().get(Constants.IMAGE_LARGE).getText())
                .into(cover);

        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true;
        final PopupWindow popupWindow = new PopupWindow(albumDetails, width, height, focusable);
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

        popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
    }


    @SuppressLint("ResourceType")
    private void initTags(Album album, View rootLayout) {
        TagView firstTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView secondTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView thirdTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView fourthTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);
        TagView fifthTag = (TagView) rootLayout.findViewById(R.id.popup_first_tag);

        TagView[] tags = new TagView[]{
                firstTag, secondTag, thirdTag, fourthTag, fifthTag
        };


        List<Tag> tagText = album.getTags().getTag();

        if(tagText.isEmpty()){
            return;
        }

        //this is very ugly, however since there is no sort lambda what can you do
        List<String> firstFive = new ArrayList<String>() {
            {
                for (int i = 0; i < tagText.size(); i++) {
                    this.add(tagText.get(i).getName());
                }
            }
{'fraction_non_alphanumeric': 0.09374566634308695, 'fraction_numerical': 0.0029122174455692693, 'mean_word_length': 2.1916353175481302, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '140735', 'n_tokens_mistral': 4043, 'n_tokens_neox': 3760, 'n_words': 707}