package com.ved.musicmapapp.fragments;
import java.util.ArrayList;
import java.util.Collections;
import com.andraskindler.quickscroll.QuickScroll;
import com.ved.musicmapapp.MainActivity;
import com.ved.musicmapapp.R;
import com.ved.musicmapapp.MainActivity.OnShareListener;
import com.ved.musicmapapp.Objects.Artist;
import com.ved.musicmapapp.Objects.Genre;
import com.ved.musicmapapp.Objects.User;
import com.ved.musicmapapp.adapters.GenreAdapter;
import com.ved.musicmapapp.utils.Statics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class GenreFragment extends Fragment implements OnShareListener{
User user;
private Activity mContext;
ListView listView;
GenreAdapter adapter;
ArrayList<String> allGenres;
private QuickScroll quickscroll;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.genre_fragment, container, false);
listView = (ListView) v.findViewById(R.id.listView);
quickscroll = (QuickScroll) v.findViewById(R.id.quickscroll);
allGenres = user.getGenre();
//
// for(int i = 0;i<user.getArtists().size();i++){
// Artist arts = user.getArtists().get(i);
// for(int j = 0;j<arts.getGenre().size();j++){
// ArrayList<Genre> g = arts.getGenre();
// for(Genre gg : g){
// if(!allGenres.toString().toLowerCase().contains(gg.getGenreName().toLowerCase()))
// allGenres.add(Character.toUpperCase(gg.getGenreName().trim().charAt(0)) + gg.getGenreName().trim().substring(1));
// }
// }
// }
if(allGenres == null || (allGenres != null && allGenres.size() == 0)){
allGenres = new ArrayList<String>();
for(int i = 0;i<user.getArtists().size();i++){
Artist arts = user.getArtists().get(i);
for(int j = 0;j<arts.getGenre().size();j++){
ArrayList<String> g = arts.getGenreNames();
for(String gg : g){
if(!allGenres.toString().toLowerCase().contains(gg.toLowerCase()))
allGenres.add(Character.toUpperCase(gg.trim().charAt(0)) + gg.trim().substring(1));
}
}
}
user.setGenre(allGenres);
}
Collections.sort(allGenres.subList(0, allGenres.size()));
adapter = new GenreAdapter(mContext, allGenres);
listView.setAdapter(adapter);
quickscroll.init(QuickScroll.TYPE_INDICATOR_WITH_HANDLE, listView, adapter, QuickScroll.STYLE_HOLO);
quickscroll.setFixedSize(1);
quickscroll.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String genre = adapter.getItem(arg2);
GenreArtistsFragment fragment = new GenreArtistsFragment();
fragment.setUser(user);
fragment.setGenre(genre);
if(getParentFragment().getClass().equals(UserDetailsFragment.class))
((UserDetailsFragment)getParentFragment()).update(fragment, true);
}
});
return v;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
this.mContext = activity;
}
public static Fragment newInstance(User user) {
GenreFragment fragment = new GenreFragment();
fragment.setUser(user);
return fragment;
}
public void setUser(User user) {
this.user = user;
}
@Override
public void onResume() {
super.onResume();
if (this.user != null && this.user.isFollowed()) {
((MainActivity) mContext).showShareAction(this);
}
else
((MainActivity) mContext).hideShareAction();
}
@Override
public void onShare() {
String sharedUrl = Statics.WEB_APP_URL + user.getUserName();
String shareBody;
if (Statics.curUser.getFbid() == user.getFbid()) {
if (user.getArtistcount() == 1) {
shareBody = getString(R.string.share_user_current_user_, user
.getArtists().get(0).getName());
} else {
shareBody = getString(R.string.share_user_current_user, user
.getArtists().get(0).getName(), user.getArtists()
.size() - 1);
}
} else {
if (user.getArtistcount() == 1) {
shareBody = getString(R.string.share_user_other_user_,
user.getFirstName(), user.getArtists().get(0).getName());
} else {
shareBody = getString(R.string.share_user_other_user,
user.getFirstName(),
user.getArtists().get(0).getName(), user.getArtists()
.size() - 1);
}
}
shareBody += "\n" + sharedUrl;
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share"));
}
}