package ai.pai.client.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.common.utils.NetworkUtil;
import com.android.views.progressbar.ProgressWheel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.PhotoFullScreenActivity;
import ai.pai.client.beans.GroupPhotoItem;
import ai.pai.client.utils.PhotoLoader;

/**
 * Created by chengzhenyu on 2015/12/11.
 */
public class DetailPhotoPageAdapter extends PagerAdapter {

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

    public DetailPhotoPageAdapter(Context context, ArrayList<GroupPhotoItem> photoList) {
        this.context = context;
        this.photoList = photoList;
        inflater = LayoutInflater.from(context);
        options = PhotoLoader.getPhotoOptions();
    }

    @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_photo, view, false);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.iv_photo_item);
        final ProgressWheel spinner = (ProgressWheel) imageLayout.findViewById(R.id.pb_loading);
        final Button reloadBtn = (Button) 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);
        imageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GroupPhotoItem photoItem = photoList.get(position);
                Intent intent = new Intent(context, PhotoFullScreenActivity.class);
                intent.putExtra("list",photoList);
                intent.putExtra("photo_item",photoItem);
                context.startActivity(intent);
            }
        });
        return imageLayout;
    }

    private void loadImage(int position, final ImageView imageView, final ProgressWheel spinner, final Button reloadBtn){
        PhotoLoader.getInstance(context).displayImage(NetworkUtil.isWifiConnected(context) ? photoList.get(position).bigThumbnailUrl :photoList.get(position).thumbnailUrl, 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) {
                Toast.makeText(view.getContext(), R.string.photo_load_fail, Toast.LENGTH_SHORT).show();
                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;
    }
}
