package ai.pai.client.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.views.tabstrip.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ai.pai.client.R;
import ai.pai.client.beans.Album;
import ai.pai.client.fragments.AlbumFragment;
import ai.pai.client.utils.FileUtils;
import ai.pai.client.utils.ImageUtils;
import ai.pai.client.utils.StringUtils;
import ai.pai.client.utils.SystemUtils;

public class PhotoGalleryActivity extends FragmentActivity{

    private ViewPager pager;
    private PagerSlidingTabStrip tab;
    private Map<String, Album> albums;
    private FragmentPagerAdapter pagerAdapter;
    private List<String> paths = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_gallery);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        TextView title = (TextView)findViewById(R.id.title_bar_middle_txt);
        title.setText(R.string.local_photos);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
        pager = (ViewPager)findViewById(R.id.pager);
        tab = (PagerSlidingTabStrip)findViewById(R.id.indicator);
        albums = ImageUtils.findGalleries(this, paths);
        if(albums.isEmpty()){
            Toast.makeText(this,R.string.no_photo_found, Toast.LENGTH_LONG).show();
            return;
        }
        pagerAdapter = new TabPageIndicatorAdapter(getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);
        tab.setViewPager(pager);
    }


    class TabPageIndicatorAdapter extends FragmentPagerAdapter {

        private ArrayList<AlbumFragment> fragmentList;
        public TabPageIndicatorAdapter(FragmentManager fm) {
            super(fm);
            fragmentList = new ArrayList<AlbumFragment>();
            for(String path:paths){
                AlbumFragment fragment = AlbumFragment.newInstance(albums.get(path).getPhotos());
                fragmentList.add(fragment);
            }
        }

        @Override
        public Fragment getItem(int position) {
              return fragmentList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Album album = albums.get(paths.get(position % paths.size()));
            if (StringUtils.equalsIgnoreCase(FileUtils.getInst().getSystemPhotoPath(),
                    album.getAlbumUri())) {
                return "胶卷相册";
            } else if (album.getTitle().length() > 13) {
                return album.getTitle().substring(0, 11) + "...";
            }
            return album.getTitle();
        }

        @Override
        public int getCount() {
            return paths.size();
        }
    }

}
