package ai.pai.client.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.common.utils.LogHelper;
import com.android.views.PhotoView.TouchImageView;
import com.android.views.progressbar.ProgressWheel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;

import ai.pai.client.BuildConfig;
import ai.pai.client.R;
import ai.pai.client.beans.GroupPhotoItem;
import ai.pai.client.utils.PhotoLoader;
import ai.pai.client.views.HackyViewPager;

public class FullScreenPhotoPageAdapter extends PagerAdapter implements TouchImageView.ZoomModeChangeListener{

    private  ArrayList<GroupPhotoItem> photoList;
    private Context context;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    private HackyViewPager pager;

    public FullScreenPhotoPageAdapter(Context context, ArrayList<GroupPhotoItem> photoList, HackyViewPager pager) {
        this.context = context;
        this.photoList = photoList;
        this.pager = pager;
        inflater = LayoutInflater.from(context);
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.default_avatar)
                .showImageOnFail(R.drawable.default_avatar)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.NONE)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

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

    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_touchable_photo, view, false);
        final TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.iv_photo_item);
        imageView.setZoomModeChangeListener(this);
        final ProgressWheel spinner = (ProgressWheel) imageLayout.findViewById(R.id.pb_loading);
        final ImageView reloadBtn = (ImageView) imageLayout.findViewById(R.id.btn_reload_photo);
        reloadBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                loadImage(position,imageView,spinner,reloadBtn);
            }
        });
        loadImage(position,imageView,spinner,reloadBtn);
        view.addView(imageLayout, 0);
        return imageLayout;
    }

    private void loadImage(int position, final ImageView imageView, final ProgressWheel spinner, final ImageView reloadBtn){
        String photoUrl = photoList.get(position).rawPhotoUrl;
        if(TextUtils.isEmpty(photoUrl)){
            photoUrl = photoList.get(position).mediumPhotoUrl;
        }
        if(TextUtils.isEmpty(photoUrl)){
            photoUrl = photoList.get(position).photoUrl;
        }
        if(TextUtils.isEmpty(photoUrl)){
            photoUrl = photoList.get(position).bigThumbnailUrl;
        }
        if(TextUtils.isEmpty(photoUrl)){
            photoUrl = photoList.get(position).thumbnailUrl;
        }
        if(BuildConfig.isDevMode){
            LogHelper.dfile("czy","\n全屏页显示图片的链接地址为 "+photoUrl);
        }
        PhotoLoader.getInstance(context).displayImage(photoUrl, imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                spinner.setVisibility(View.VISIBLE);
                spinner.startSpinning();
                reloadBtn.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                spinner.stopSpinning();
                spinner.setVisibility(View.GONE);
                reloadBtn.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.stopSpinning();
                spinner.setVisibility(View.GONE);
                reloadBtn.setVisibility(View.GONE);
            }
        });
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void onZoomModeChanged(boolean isZoomed) {
        pager.setLocked(isZoomed);
    }
}
