package byc.imagewatcher;

import android.app.Activity;
import android.net.Uri;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.List;

import byc.imagewatcher.view.imagewatcher.R;


public class ImageWatcherHelper {
    private static final int VIEW_IMAGE_WATCHER_ID = R.id.view_image_watcher;
    private final Activity holder;
    private final ViewGroup activityDecorView;
    private ImageWatcher mImageWatcher;
    private ImageWatcher.Loader loader;
    private Integer statusBarHeight;
    private Integer resErrorImage;
    private ImageWatcher.OnPictureLongPressListener listener;
    private ImageWatcher.IndexProvider indexProvider;
    private ImageWatcher.LoadingUIProvider loadingUIProvider;
    private ImageWatcher.OnStateChangedListener onStateChangedListener;

    private ImageWatcherHelper(Activity activity) {
        holder = activity;
        activityDecorView = (ViewGroup) activity.getWindow().getDecorView();
    }

    public static ImageWatcherHelper with(Activity activity, ImageWatcher.Loader l) { // attach
        if (activity == null) throw new NullPointerException("activity is null");
        if (l == null) throw new NullPointerException("loader is null");
        ImageWatcherHelper iwh = new ImageWatcherHelper(activity);
        iwh.loader = l;
        return iwh;
    }

    public ImageWatcherHelper setTranslucentStatus(int statusBarHeight) {
        this.statusBarHeight = statusBarHeight;
        return this;
    }

    public ImageWatcherHelper setErrorImageRes(int resErrorImage) {
        this.resErrorImage = resErrorImage;
        return this;
    }

    public ImageWatcherHelper setOnPictureLongPressListener(ImageWatcher.OnPictureLongPressListener listener) {
        this.listener = listener;
        return this;
    }

    public ImageWatcherHelper setIndexProvider(ImageWatcher.IndexProvider ip) {
        indexProvider = ip;
        return this;
    }

    public ImageWatcherHelper setLoadingUIProvider(ImageWatcher.LoadingUIProvider lp) {
        loadingUIProvider = lp;
        return this;
    }

    public ImageWatcherHelper setOnStateChangedListener(ImageWatcher.OnStateChangedListener listener) {
        onStateChangedListener = listener;
        return this;
    }

    public OnImageLikeClickListener onImageLikeClickListener;
    public List<String> data;

    public void show(ImageView i, SparseArray<ImageView> imageGroupList, List<Uri> urlList, List<String> data, OnImageLikeClickListener onImageLikeClickListener) {
        this.onImageLikeClickListener = onImageLikeClickListener;
        this.data = data;
        init();
        mImageWatcher.show(i, imageGroupList, urlList);
    }

    public void updateLikeStatus(int pos, boolean b) {
        mImageWatcher.updateLikeStatus(pos, b);
    }

    public void show(List<Uri> urlList, int initPos) {
        init();
        mImageWatcher.show(urlList, initPos);
    }

    private void init() {
        mImageWatcher = new ImageWatcher(holder);
        mImageWatcher.setId(VIEW_IMAGE_WATCHER_ID);
        mImageWatcher.setLoader(loader);
        mImageWatcher.setImageLikeClickListenerAndData(data, onImageLikeClickListener);
        mImageWatcher.setDetachAffirmative(); // helper
        if (statusBarHeight != null) mImageWatcher.setTranslucentStatus(statusBarHeight);
        if (resErrorImage != null) mImageWatcher.setErrorImageRes(resErrorImage);
        if (listener != null) mImageWatcher.setOnPictureLongPressListener(listener);
        if (indexProvider != null) mImageWatcher.setIndexProvider(indexProvider);
        if (loadingUIProvider != null) mImageWatcher.setLoadingUIProvider(loadingUIProvider);
        if (onStateChangedListener != null)
            mImageWatcher.setOnStateChangedListener(onStateChangedListener);

        removeExistingOverlayInView(activityDecorView); // 理论上是无意义的操作。在ImageWatcher 'dismiss' 时会移除自身。但检查一下不错

//        final ImageView imglike = new ImageView(holder.getBaseContext());
//
//        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER;
//        imglike.setLayoutParams(layoutParams);
//        imglike.setImageResource(R.mipmap.icon_like_default_user);

        activityDecorView.addView(mImageWatcher);
      //  activityDecorView.addView(imglike);
    }

    public boolean handleBackPressed() {
        return mImageWatcher != null && mImageWatcher.handleBackPressed();
    }

    private void removeExistingOverlayInView(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            if (child.getId() == VIEW_IMAGE_WATCHER_ID) {
                parent.removeView(child);
            }
            if (child instanceof ViewGroup) {
                removeExistingOverlayInView((ViewGroup) child);
            }
        }
    }

    public interface Provider {
        ImageWatcherHelper iwHelper();
    }
}
