/*
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * 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.namelessdev.mpdroid.fragments;

import com.namelessdev.mpdroid.MPDApplication;
import com.namelessdev.mpdroid.R;
import com.namelessdev.mpdroid.library.ILibraryFragmentActivity;
import com.namelessdev.mpdroid.tools.Tools;

import org.a0z.mpd.MPDCommand;
import org.a0z.mpd.exception.MPDException;
import org.a0z.mpd.item.Artist;
import org.a0z.mpd.item.Genre;
import org.a0z.mpd.item.Item;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.StringRes;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import java.io.IOException;

public class ArtistsFragment extends BrowseFragment {

    private static final String TAG = "ArtistsFragment";

    private Genre mGenre = null;

    public ArtistsFragment() {
        super(R.string.addArtist, R.string.artistAdded, MPDCommand.MPD_SEARCH_ARTIST);
    }

    @Override
    protected void add(final Item item, final boolean replace, final boolean play) {
        try {
            mApp.oMPDAsyncHelper.oMPD.add((Artist) item, replace, play);
            if (isAdded()) {
                Tools.notifyUser(mIrAdded, item);
            }
        } catch (final IOException | MPDException e) {
            Log.e(TAG, "Failed to add to queue.", e);
        }
    }

    @Override
    protected void add(final Item item, final String playlist) {
        try {
            mApp.oMPDAsyncHelper.oMPD.addToPlaylist(playlist, (Artist) item);
            if (isAdded()) {
                Tools.notifyUser(mIrAdded, item);
            }
        } catch (final IOException | MPDException e) {
            Log.e(TAG, "Failed to add to playlist.", e);
        }
    }

    @Override
    protected void asyncUpdate() {
        try {
            final SharedPreferences settings = PreferenceManager
                    .getDefaultSharedPreferences(MPDApplication.getInstance());
            switch (settings.getString(LibraryFragment.PREFERENCE_ARTIST_TAG_TO_USE,
                    LibraryFragment.PREFERENCE_ARTIST_TAG_TO_USE_BOTH).toLowerCase()) {
                case LibraryFragment.PREFERENCE_ARTIST_TAG_TO_USE_ALBUMARTIST:
                    if (mGenre != null) {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists(mGenre, true);
                    } else {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists(true);
                    }
                    break;
                case LibraryFragment.PREFERENCE_ARTIST_TAG_TO_USE_ARTIST:
                    if (mGenre != null) {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists(mGenre, false);
                    } else {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists(false);
                    }
                    break;
                case LibraryFragment.PREFERENCE_ARTIST_TAG_TO_USE_BOTH:
                default:
                    if (mGenre != null) {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists(mGenre);
                    } else {
                        mItems = mApp.oMPDAsyncHelper.oMPD.getArtists();
                    }
                    break;
            }
        } catch (final IOException | MPDException e) {
            Log.e(TAG, "Failed to update.", e);
        }
    }

    @Override
    @StringRes
    public int getLoadingText() {
        return R.string.loadingArtists;
    }

    @Override
    public String getTitle() {
        if (mGenre != null) {
            return mGenre.mainText();
        } else {
            return getString(R.string.genres);
        }
    }

    public ArtistsFragment init(final Genre g) {
        mGenre = g;
        return this;
    }

    @Override
    public void onItemClick(final AdapterView<?> parent, final View view, final int position,
            final long id) {
        final AlbumsFragment af;
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(mApp);
        if (settings.getBoolean(LibraryFragment.PREFERENCE_ALBUM_LIBRARY, true)) {
            af = new AlbumsGridFragment((Artist) mItems.get(position), mGenre);
        } else {
            af = new AlbumsFragment((Artist) mItems.get(position), mGenre);
        }
        ((ILibraryFragmentActivity) getActivity()).pushLibraryFragment(af, "album");
    }
}

@@ -1047,24 +1047,50 @@
 references;%0A
+import android.os.Bundle;%0A
 import andro
@@ -1396,16 +1396,72 @@
 ment%22;%0A%0A
+    private static final String EXTRA_GENRE = %22genre%22;%0A%0A
     priv
@@ -4405,32 +4405,282 @@
 rn this;%0A    %7D%0A%0A
+    @Override%0A    public void onCreate(final Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        if (savedInstanceState != null) %7B%0A            init((Genre) savedInstanceState.getParcelable(EXTRA_GENRE));%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -5303,10 +5303,235 @@
 ;%0A    %7D%0A
+%0A    @Override%0A    public void onSaveInstanceState(final Bundle outState) %7B%0A        if (mGenre != null) %7B%0A            outState.putParcelable(EXTRA_GENRE, mGenre);%0A        %7D%0A        super.onSaveInstanceState(outState);%0A    %7D%0A%0A
 %7D%0A

Save and restore genre state