package com.sybotan.android.demo.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.sybotan.android.demo.R;
import com.sybotan.android.demo.tools.CommonUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * created by lihao on 2018/11/14
 * 查看大图片
 */
public class LookBigPicActivity extends BaseActivity {


    PhotoView mImageView;
    private String mUrl;
    private String mId;
    private boolean mCanEdit;
    private int position;
    //图片key
    private String mKey;
    private String mPath;
    private String localPath;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_big_pic);
        mImageView = findViewById(R.id.look_big_pic_iv);
        mUrl = getIntent().getStringExtra("url");
        mId = getIntent().getStringExtra("id");
        mKey = getIntent().getStringExtra("key");
        mPath = getIntent().getStringExtra("path");
        localPath = getIntent().getStringExtra("localPath");
        mCanEdit = getIntent().getBooleanExtra("canEdit", false);
        position = getIntent().getIntExtra("position", 0);
        type = getIntent().getIntExtra("type", 0);
        initView();
    }

    /**
     * 图片压缩
     */
    public static Bitmap getScaledBitmap(String filePath, int width) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        int sampleSize = options.outWidth > width ? options.outWidth / width
                + 1 : 1;
        options.inJustDecodeBounds = false;
        options.inSampleSize = sampleSize;
        return BitmapFactory.decodeFile(filePath, options);
    }

    private void initView() {
        if (!TextUtils.isEmpty(localPath)) {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            mImageView.setImageBitmap(getScaledBitmap(localPath, wm.getDefaultDisplay().getWidth()));
            return;
        }
        if (!TextUtils.isEmpty(mPath)) {
            Bitmap bitmap = getLoacalBitmap(mPath);
            mImageView.setImageBitmap(bitmap);
        } else {
            //GlideUtils.loadImage(this, mUrl, mImageView, false);
            if (TextUtils.isEmpty(mUrl)) {
                return;
            }
            showImgWithThumbnail(this, mUrl, 1.0f, mImageView, getIntent().getBooleanExtra("isFile", false));
        }
    }

    /**
     * 加载图片若引起oom则缩略加载
     *
     * @param context
     * @param url
     * @param thumb
     * @param mImageView
     */
    public static void showImgWithThumbnail(Context context, String url, float thumb, ImageView mImageView, boolean isFile) {
        try {
            if (isFile) {
                Glide.with(context).load(new File(url)).thumbnail(thumb).into(mImageView);
            } else {
                Glide.with(context).load(url).thumbnail(thumb).into(mImageView);
            }
        } catch (OutOfMemoryError e) {
            thumb -= 0.2f;
            if (thumb <= 0) {
                thumb = 0.1f;
            }
            showImgWithThumbnail(context, url, thumb, mImageView, isFile);
        }
    }

    /**
     * 加载本地图片
     * http://bbs.3gstdy.com
     *
     * @param url
     * @return
     */
    public static Bitmap getLoacalBitmap(String url) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            Bitmap bmp = BitmapFactory.decodeFile(url, options);
            return bmp;
        } catch (Error e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void launch(Context activity, String url, boolean canEdit) {
        activity.startActivity(new Intent(activity, LookBigPicActivity.class)
                .putExtra("url", url).putExtra("canEdit", canEdit));
    }

    public static void launch(Context activity, String url, boolean canEdit, boolean isFile) {
        activity.startActivity(new Intent(activity, LookBigPicActivity.class)
                .putExtra("url", url).putExtra("canEdit", canEdit)
                .putExtra("isFile", isFile));
    }

    public static void launch(Context activity, String url, boolean canEdit, int type, String time, String projectName, boolean isFile) {
        activity.startActivity(new Intent(activity, LookBigPicActivity.class)
                .putExtra("url", url).putExtra("canEdit", canEdit)
                .putExtra("type", type)
                .putExtra("time", time)
                .putExtra("projectName", projectName)
                .putExtra("isFile", isFile));
    }
}
