/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kymjs.core.bitmap;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;

import com.kymjs.core.bitmap.client.BitmapRequestConfig;
import com.kymjs.core.bitmap.diskloader.AsyncPoster;
import com.kymjs.core.bitmap.diskloader.BackgroundPoster;
import com.kymjs.core.bitmap.interf.IBitmapCache;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.http.Response;

import java.util.Collections;

/**
 * 本地图片加载
 *
 * @author kymjs (https://www.kymjs.com/)
 */
public class DiskImageDisplayer {

    private final Handler handle = new Handler(Looper.getMainLooper());
    private IBitmapCache mMemoryCache;
    private BackgroundPoster backgroundPoster; //串行
    private AsyncPoster asyncPoster; //并行

    public DiskImageDisplayer(IBitmapCache memoryCache) {
        if (mMemoryCache == null) {
            mMemoryCache = new BitmapMemoryCache();
        }
        this.mMemoryCache = memoryCache;
        backgroundPoster = new BackgroundPoster(this);
        asyncPoster = new AsyncPoster(this);
    }

    public void load(BitmapRequestConfig config, HttpCallback callback, boolean useAsync) {
        if (callback != null) {
            callback.onPreStart();
        }
        if (useAsync) {
            asyncPoster.enqueue(config, callback);
        } else {
            backgroundPoster.enqueue(config, callback);
        }
    }

    public void post(String path, HttpCallback callback, Response<Bitmap> response) {
        if (response != null) {
            if (response.isSuccess()) {
                doSuccess(callback, response.result);
                mMemoryCache.putBitmap(path, response.result);
            } else {
                doFailure(callback, response.error);
            }
        } else {
            doFailure(callback, new RuntimeException("load bitmap response is null"));
        }
    }

    public IBitmapCache getMemoryCache() {
        return mMemoryCache;
    }

    private void doSuccess(final HttpCallback callback, final Bitmap bitmap) {
        if (callback != null) {
            handle.post(new Runnable() {
                @Override
                public void run() {
                    callback.onSuccess(Collections.<String, String>emptyMap(), bitmap);
                    callback.onFinish();
                }
            });
        }
    }

    private void doFailure(final HttpCallback callback, final Exception e) {
        if (callback != null) {
            handle.post(new Runnable() {
                @Override
                public void run() {
                    callback.onFailure(-1, e.getMessage());
                    callback.onFinish();
                }
            });
        }
    }
}
