package com.wisewoods.eduandroid.ui.adapter.photo;

import com.blankj.utilcode.util.LogUtils;
import com.luck.picture.lib.app.PictureAppMaster;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.entity.MediaExtraInfo;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.tools.MediaUtils;

import java.lang.ref.WeakReference;
import java.util.List;

public class DefaultResultCallback implements OnResultCallbackListener<LocalMedia> {

    private WeakReference<GridImageAdapter> mAdapterWeakReference;

    public DefaultResultCallback(GridImageAdapter adapter) {
        super();
        this.mAdapterWeakReference = new WeakReference<>(adapter);
    }

    @Override
    public void onResult(List<LocalMedia> result) {
        for (LocalMedia media : result) {
            if (media.getWidth() == 0 || media.getHeight() == 0) {
                if (PictureMimeType.isHasImage(media.getMimeType())) {
                    MediaExtraInfo imageExtraInfo = MediaUtils.getImageSize(media.getPath());
                    media.setWidth(imageExtraInfo.getWidth());
                    media.setHeight(imageExtraInfo.getHeight());
                } else if (PictureMimeType.isHasVideo(media.getMimeType())) {
                    MediaExtraInfo videoExtraInfo = MediaUtils.getVideoSize(
                            PictureAppMaster.getInstance().getAppContext(), media.getPath());
                    media.setWidth(videoExtraInfo.getWidth());
                    media.setHeight(videoExtraInfo.getHeight());
                }
            }
            LogUtils.i("文件名: " + media.getFileName());
            LogUtils.i("是否压缩:" + media.isCompressed());
            LogUtils.i("压缩:" + media.getCompressPath());
            LogUtils.i("原图:" + media.getPath());
            LogUtils.i("绝对路径:" + media.getRealPath());
            LogUtils.i("是否裁剪:" + media.isCut());
            LogUtils.i("裁剪:" + media.getCutPath());
            LogUtils.i("是否开启原图:" + media.isOriginal());
            LogUtils.i("原图路径:" + media.getOriginalPath());
            LogUtils.i("Android Q 特有Path:" + media.getAndroidQToPath());
            LogUtils.i("宽高: " + media.getWidth() + "x" + media.getHeight());
            LogUtils.i("Size: " + media.getSize());

            LogUtils.i("onResult: " + media.toString());

            // TODO 可以通过PictureSelectorExternalUtils.getExifInterface();
            //  方法获取一些额外的资源信息，如旋转角度、经纬度等信息
        }
        if (mAdapterWeakReference.get() != null) {
            mAdapterWeakReference.get().setList(result);
            mAdapterWeakReference.get().notifyDataSetChanged();
        }
    }

    @Override
    public void onCancel() {
        LogUtils.i("PictureSelector Cancel");
    }
}
