package ai.pai.client.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.adapter.GalleryAdapter;
import ai.pai.client.beans.PhotoItem;


public class AlbumFragment extends BaseFragment implements AdapterView.OnItemClickListener{
    private GridView albumGridView;
    private GalleryAdapter adapter;
    private ArrayList<PhotoItem> photos = new ArrayList<PhotoItem>();

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    public static AlbumFragment newInstance(ArrayList<PhotoItem> photos) {
        AlbumFragment fragment = new AlbumFragment();
        Bundle args = new Bundle();
        args.putSerializable("photos", photos);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photos = (ArrayList<PhotoItem>) getArguments().getSerializable("photos");
        adapter = new GalleryAdapter(getActivity(), photos);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View root = inflater.inflate(R.layout.fragment_album, null);
        albumGridView = (GridView) root.findViewById(R.id.albums);
        albumGridView.setAdapter(adapter);
        albumGridView.setOnItemClickListener(this);
        return root;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("photo_path",photos.get(position).getImageUri());
        getActivity().setResult(Activity.RESULT_OK,getActivity().getIntent().putExtras(bundle));
        getActivity().finish();
    }
}
