content: /*
   * Copyright 2014 Soichiro Kashima
   *
   * 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.wm.remusic.fragmentnet;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  import com.bilibili.magicasakura.widgets.TintImageView;
  import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
  import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
  import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
  import com.wm.remusic.R;
  import com.wm.remusic.fragment.BaseFragment;
  import com.wm.remusic.fragment.MoreFragment;
  import com.wm.remusic.handler.HandlerUtil;
  import com.wm.remusic.info.MusicInfo;
  import com.wm.remusic.service.MusicPlayer;
  import com.wm.remusic.uitl.IConstants;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  
  public class ArtistInfoMusicFragment extends BaseFragment {
      ArrayList<MusicInfo> mList = new ArrayList<>();
      PlaylistDetailAdapter mAdapter;
  
      public static Fragment getInstance(ArrayList<MusicInfo> mList) {
          ArtistInfoMusicFragment fragment = new ArtistInfoMusicFragment();
          Bundle bundle = new Bundle();
          bundle.putParcelableArrayList("list", mList);
          fragment.setArguments(bundle);
  
          return fragment;
      }
  
      public static final String ARG_INITIAL_POSITION = "ARG_INITIAL_POSITION";
      ObservableRecyclerView recyclerView;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
  
          if (getArguments() != null) {
              mList = getArguments().getParcelableArrayList("list");
          }
          Activity parentActivity = getActivity();
          recyclerView = (ObservableRecyclerView) view.findViewById(R.id.scroll);
          recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
          recyclerView.setHasFixedSize(true);
          mAdapter = new PlaylistDetailAdapter(getActivity(), mList);
          recyclerView.setAdapter(mAdapter);
  
          if (parentActivity instanceof ObservableScrollViewCallbacks) {
              // Scroll to the specified offset after layout
              Bundle args = getArguments();
              if (args != null && args.containsKey(ARG_INITIAL_POSITION)) {
                  final int initialPosition = args.getInt(ARG_INITIAL_POSITION, 0);
                  ScrollUtils.addOnGlobalLayoutListener(recyclerView, new Runnable() {
                      @Override
                      public void run() {
                          //  recyclerView.scrollVerticallyToPosition(0);
                      }
                  });
              }
  
              // TouchInterceptionViewGroup should be a parent view other than ViewPager.
              // This is a workaround for the issue #117:
              // https://github.com/ksoichiro/Android-ObservableScrollView/issues/117
              recyclerView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.root));
  
              recyclerView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
          }
          return view;
      }
  
      @Override
      public void updateTrackInfo() {
          super.updateTrackInfo();
          mAdapter.notifyDataSetChanged();
      }
  
      class PlaylistDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
          final static int FIRST_ITEM = 0;
          final static int ITEM = 1;
          private ArrayList<MusicInfo> arraylist;
          private Activity mContext;
  
          public PlaylistDetailAdapter(Activity context, ArrayList<MusicInfo> mList) {
              this.arraylist = mList;
              this.mContext = context;
          }
  
          @Override
          public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
              if (viewType == FIRST_ITEM) {
                  return new CommonItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.artist_header_common_item, viewGroup, false));
              } else {
                  return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_playlist_detail_item, viewGroup, false));
              }
          }
  
          //判断布局类型
          @Override
          public int getItemViewType(int position) {
              return position == FIRST_ITEM ? FIRST_ITEM : ITEM;
  
          }
  
          @Override
          public void onBindViewHolder(final RecyclerView.ViewHolder itemHolder, final int i) {
              if (itemHolder instanceof ItemViewHolder) {
                  final MusicInfo localItem = arraylist.get(i - 1);
                  //判断该条目音乐是否在播放
                  if (MusicPlayer.getCurrentAudioId() == localItem.songId) {
                      ((ItemViewHolder) itemHolder).trackNumber.setVisibility(View.GONE);
                      ((ItemViewHolder) itemHolder).playState.setVisibility(View.VISIBLE);
                      ((ItemViewHolder) itemHolder).playState.setImageResource(R.drawable.song_play_icon);
                      ((ItemViewHolder) itemHolder).playState.setImageTintList(R.color.theme_color_primary);
                  } else {
                      ((ItemViewHolder) itemHolder).playState.setVisibility(View.GONE);
                      ((ItemViewHolder) itemHolder).trackNumber.setVisibility(View.VISIBLE);
                      ((ItemViewHolder) itemHolder).trackNumber.setText(i + "");
                  }
                  ((ItemViewHolder) itemHolder).title.setText(localItem.musicName);
                  ((ItemViewHolder) itemHolder).artist.setText(localItem.artist);
                  ((ItemViewHolder) itemHolder).menu.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
  
  //                        if(localItem.islocal){
                          MoreFragment morefragment = MoreFragment.newInstance(arraylist.get(i - 1).songId + "",
                                  IConstants.MUSICOVERFLOW);
                          morefragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "music");
  //                            return;
  //                        }
  //                        new AlertDialog.Builder(mContext).setTitle("要下载音乐吗").
  //                                setPositiveButton(mContext.getString(R.string.sure), new DialogInterface.OnClickListener() {
  //
  //                                    @Override
  //                                    public void onClick(DialogInterface dialog, int which) {
  //
  //                                        Down.downMusic(MainApplication.context, localItem.songId + "", localItem.musicName);
  //                                        dialog.dismiss();
  //                                    }
  //                                }).
  //                                setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
  //                                    @Override
  //                                    public void onClick(DialogInterface dialog, int which) {
  //                                        dialog.dismiss();
  //                                    }
  //                                }).show();
                      }
                  });
  
              } else if (itemHolder instanceof CommonItemViewHolder) {
  
                  ((CommonItemViewHolder) itemHolder).textView.setText("(共" + arraylist.size() + "首)");
  
                  ((CommonItemViewHolder) itemHolder).select.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
  
                      }
                  });
  
              }
  
          }
  
          @Override
          public int getItemCount() {
              return arraylist == null ? 0 : arraylist.size() + 1;
          }
  
          public void updateDataSet(ArrayList<MusicInfo> arraylist) {
              this.arraylist = arraylist;
              this.notifyDataSetChanged();
          }
  
          public class CommonItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
              TextView textView;
              ImageView select;
              RelativeLayout layout;
  
              CommonItemViewHolder(View view) {
                  super(view);
                  this.textView = (TextView) view.findViewById(R.id.play_all_number);
                  this.select = (ImageView) view.findViewById(R.id.select);
                  this.layout = (RelativeLayout) view.findViewById(R.id.play_all_layout);
                  layout.setOnClickListener(this);
              }
  
              public void onClick(View v) {
                  //// TODO: 2016/1/20
                  HandlerUtil.getInstance(mContext).postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          HashMap<Long, MusicInfo> infos = new HashMap<Long, MusicInfo>();
                          int len = arraylist.size();
                          long[] list = new long[len];
                          for (int i = 0; i < len; i++) {
                              MusicInfo info = arraylist.get(i);
                              list[i] = info.songId;
                              infos.put(list[i], info);
                          }
                          MusicPlayer.playAll(infos, list, 0, false);
                      }
                  },70);
              }
  
          }
  
          public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
              protected TextView title, artist, trackNumber;
              protected ImageView menu;
              TintImageView playState;
  
              public ItemViewHolder(View view) {
                  super(view);
                  this.title = (TextView) view.findViewById(R.id.song_title);
                  this.artist = (TextView) view.findViewById(R.id.song_artist);
                  this.trackNumber = (TextView) view.findViewById(R.id.trackNumber);
                  this.menu = (ImageView) view.findViewById(R.id.popup_menu);
                  this.playState = (TintImageView) view.findViewById(R.id.play_state);
                  view.setOnClickListener(this);
              }
  
              @Override
              public void onClick(View v) {
                  HandlerUtil.getInstance(mContext).postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          HashMap<Long, MusicInfo> infos = new HashMap<Long, MusicInfo>();
                          int len = arraylist.size();
                          long[] list = new long[len];
                          for (int i = 0; i < len; i++) {
                              MusicInfo info = arraylist.get(i);
                              list[i] = info.songId;
                              infos.put(list[i], info);
                          }
                          if (getAdapterPosition() > 0)
                              MusicPlayer.playAll(infos, list, getAdapterPosition() - 1, false);
                      }
                  }, 70);
              }
  
          }
      }
  
  
  }
  