package com.phlx.anchorcollect.util;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.yancy.gallerypick.config.GalleryConfig;
import com.yancy.gallerypick.config.GalleryPick;
import com.yancy.gallerypick.inter.IHandlerCallBack;
import com.yancy.gallerypick.inter.ImageLoader;
import com.yancy.gallerypick.widget.GalleryImageView;

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

public class ImagePicker {

    private static final String TAG = ImagePicker.class.getName();
    private static final String DEFAULT_DIR = "/tracing/camera";
    private IHandlerCallBack iHandlerCallBack;
    private GalleryConfig galleryConfig;
    private List<String> path = new ArrayList<>();
    private OnImagePickerListener listener;


    private Context mContext;


    public ImagePicker(Context context) {

        this.mContext = context;

        initGallery();
    }

    private class GlideImageLoader implements ImageLoader {
        @Override
        public void displayImage(Activity activity, Context context, String path, GalleryImageView galleryImageView, int width, int
                height) {
            Glide.with(context).load(path).into(galleryImageView);
        }

        @Override
        public void clearMemoryCache() {

        }
    }

    private void initGallery() {
        iHandlerCallBack = new IHandlerCallBack() {
            @Override
            public void onStart() {
                Log.i(TAG, "onStart: 开启");
            }

            @Override
            public void onSuccess(List<String> photoList) {
                Log.i(TAG, "onSuccess: 返回数据");
                path.clear();
                for (String s : photoList) {
                    Log.i(TAG, s);
                    path.add(s);
                }
                if (listener != null && path.size() > 0) {
                    listener.onSuccess(path.get(0));
                }
            }

            @Override
            public void onCancel() {
                Log.i(TAG, "onCancel: 取消");
            }

            @Override
            public void onFinish() {
                Log.i(TAG, "onFinish: 结束");
            }

            @Override
            public void onError() {
                Log.i(TAG, "onError: 出错");
            }
        };

        galleryConfig = new GalleryConfig.Builder().imageLoader(new GlideImageLoader())    // ImageLoader 加载框架（必填）
                .iHandlerCallBack(iHandlerCallBack)     // 监听接口（必填）
                .provider(mContext.getPackageName() + ".file_provider")   // provider (必填)
                .multiSelect(false)
                .isOpenCamera(true)                    // 是否现实相机按钮  默认：false
                .filePath(DEFAULT_DIR)          // 图片存放路径
                .build();

    }

    public void show(Activity activity) {
        GalleryPick.getInstance().setGalleryConfig(galleryConfig).open(activity);
    }

    public void setOnImagePickerListener(OnImagePickerListener listener) {
        this.listener = listener;
    }

    public interface OnImagePickerListener {
        void onSuccess(String path);
    }


}
