package community.peers.internetradio.fragment;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import community.peers.internetradio.activity.MainActivity.StationListItem;
import community.peers.internetradio.R;
import community.peers.internetradio.storage.Settings;


public class PlayedFragment extends Fragment
{
    Activity fragmentActivity;
    ListView stations_listview;
    FragmentListAdapter simpleAdapter;

    ArrayList<HashMap<String, String>> lastPlayed;
    StationListItem clickCallback;

    public PlayedFragment()
    {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // The activity of this fragment
        this.fragmentActivity = this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View thisView = inflater.inflate(R.layout.fragment_played, container, false);

        this.stations_listview = (ListView) thisView.findViewById(R.id.played_list);

        // Retrieve list of radios
        this.loadLastPlayed();

        // Inflate the layout for this fragment
        return thisView;
    }

    @Override
    public void onActivityCreated(Bundle bundle)
    {
        super.onActivityCreated(bundle);
    }

    public void setClickCallback(StationListItem theCallback)
    {
        this.clickCallback = theCallback;
    }

    // Retrieve list from app preferences
    public void loadLastPlayed()
    {
        this.lastPlayed = Settings.retrieveLastPlayed(this.fragmentActivity);

        // Inflate ListView
        String[] from = {"name"};
        int[] to = { R.id.radio_name };

        this.simpleAdapter = new FragmentListAdapter(
            this.fragmentActivity,
            lastPlayed,
            R.layout.radio_list_item,
            from, to);

        this.stations_listview.setAdapter(this.simpleAdapter);

        // Click handler for list item
        this.stations_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                HashMap<String, String> thisRadio = lastPlayed.get(position);

                if (clickCallback != null)
                    clickCallback.stream(thisRadio);
            }
        });

        // Long-Click handler for list item
        this.stations_listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                String streamUrl = lastPlayed.get(position).get("stream");

                // Copy the radio stream to the clipboard.
                ClipboardManager clipboardManager = (ClipboardManager) view.getContext()
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("Radio stream", streamUrl);
                clipboardManager.setPrimaryClip(clip);
                Toast.makeText(view.getContext(),
                        view.getContext().getResources().getString(R.string.copy_clipboard),
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}