package com.stu.assistant.uitls;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stu.assistant.R;
import com.stu.assistant.base.BaseApplion;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ImageUtil {

    public static void showImageView(Context context, int url,
                                     ImageView imgeview) {

        Glide.with(context).load(url).into(imgeview);
    }

    public static void showImageView(Context context, String url,
                                     ImageView imgeview, int error) {

        RequestOptions options = new RequestOptions();
        options.error(error);
        Glide.with(context).load(url).apply(options).into(imgeview);
    }

    public static void showThumbnailImageView(Context context, String url,
                                              ImageView imgeview, int error) {
        RequestOptions options = new RequestOptions();
        options.error(error);
//        Glide.with(context).load(url).apply(options).thumbnail(Glide.with(context).load(R.mipmap.gif_ic)).into(imgeview);
    }

    public static void showImageView(Context context, String url,
                                     ImageView imgeview) {

        try {
            RequestOptions options = new RequestOptions();
            options.error(R.mipmap.loadfail_img);
            Glide.with(context).load(TextUtils.isEmpty(url) == true ? "":url).apply(options).into(imgeview);
        }catch (Exception e){
            e.fillInStackTrace();
        }
    }

    public static void showImageViewRadius(Context context, String url,
                                     ImageView imgeview,int radus) {

        RoundedCorners roundedCorners= new RoundedCorners(radus);

        RequestOptions options=RequestOptions.bitmapTransform(roundedCorners);
        options.error(R.mipmap.loadfail_img);

        Glide.with(context).load(url).apply(options).into(imgeview);
    }

    public static void showImageViewNotErr(Context context, String url,
                                           ImageView imgeview) {

        RequestOptions options = new RequestOptions();
        Glide.with(context).load(url).apply(options).into(imgeview);
    }


    public static void showImageView(Context context, String url, int error,
                                     ImageView imgeview, boolean isCircle) {


        if (isCircle) {
            Glide
                    .with(context)
                    .load(url)
                    .apply(new RequestOptions().circleCrop().placeholder(error))
                    .into(imgeview);
        } else {
            Glide
                    .with(context)
                    .load(url)
                    .apply(new RequestOptions().placeholder(error))
                    .into(imgeview);
        }
    }
    public static void showImageViewCircle(int url, ImageView imgeview) {

        Glide
                .with(BaseApplion.application)
                .load(url)
                .apply(new RequestOptions().circleCrop().placeholder(R.mipmap.loadfail_img))
                .into(imgeview);
    }


    public static Bitmap getBitmapFormUri(Context context, Uri uri) throws FileNotFoundException, IOException {
        InputStream input = context.getContentResolver().openInputStream(uri);

        //这一段代码是不加载文件到内存中也得到bitmap的真是宽高，主要是设置inJustDecodeBounds为true
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;//不加载到内存
        onlyBoundsOptions.inDither = true;//optional
        onlyBoundsOptions.inPreferredConfig = Bitmap.Config.RGB_565;//optional
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        input.close();
        int originalWidth = onlyBoundsOptions.outWidth;
        int originalHeight = onlyBoundsOptions.outHeight;
        if ((originalWidth == -1) || (originalHeight == -1)) {
            return null;
        }

        //图片分辨率以480x800为标准
        float hh = 1920f;//这里设置高度为800f
        float ww = 1080f;//这里设置宽度为480f
        //缩放比，由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (originalWidth / ww);
        } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (originalHeight / hh);
        }
        if (be <= 0) {
            be = 1;
        }
        //比例压缩
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = be;//设置缩放比例
        bitmapOptions.inDither = true;
        bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        input = context.getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();

        return compressImage(bitmap);//再进行质量压缩
    }

    public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 500) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            //第一个参数 ：图片格式 ，第二个参数： 图片质量，100为最高，0为最差  ，第三个参数：保存压缩后的数据的流
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options，把压缩后的数据存放到baos中
            options -= 10;//每次都减少10
            if (options <= 0) {
                break;
            }
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
        return bitmap;
    }

    public static File saveFile(Bitmap bm, String path) throws IOException {//将Bitmap类型的图片转化成file类型，便于上传到服务器
        File myCaptureFile = new File(path);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return myCaptureFile;

    }

    //照片加水印
    public File saveFile2(Bitmap bitmap, String path) throws IOException {//将Bitmap类型的图片转化成file类型，便于上传到服务器
        File myCaptureFile = new File(path);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return myCaptureFile;

    }

    private String getWaterMarkMsg() {
        SimpleDateFormat dff = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String date = "";
        try {
            dff.setTimeZone(TimeZone.getTimeZone("GMT+08"));
            date = dff.format(new Date());
        } catch (Exception e) {
            e.fillInStackTrace();
            long currentTime = System.currentTimeMillis();
            SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            date = formatter.format(currentTime);
        }
        return date;
    }


    public static synchronized String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 查看大图
     *
     * @param
     */
//    public static void showBigImg(final Context context, final List<String> imglist, View parent_view, final int position) {
//        pop = new PopWindowUtils((Activity) context);
//        pop.setFocusable(true);
//        pop.setOutsideTouchable(true);
//        pop.setOnViewClickListener(new PopWindowUtils.ContentView() {
//            @Override
//            public void getContentView(View contentView, PopWindowUtils popWindow) {
//                pop.setWidth(AppTools.getScreenWidth(context));
//                pop.setHeight(AppTools.getScreenHeight(context));
//                ViewPager imgviewPager = contentView.findViewById(R.id.imgviewPager);
//                ImgPagerAdapter imgPagerAdapter = new ImgPagerAdapter(context, imglist);
//                imgviewPager.setAdapter(imgPagerAdapter);
//                imgviewPager.setCurrentItem(position);
//                imgviewPager.setOffscreenPageLimit(1);//限定预加载
//                imgPagerAdapter.setImgItemCliclListener(position1 -> pop.dismiss());
//                //返回
//                imgPagerAdapter.setBackCliclListener(() -> pop.dismiss());
//
//            }
//
//
//        }).setConView(R.layout.browse_bigimg_view).showBrowseImgnCenter(parent_view);
//
//    }




    /**
     * 读取图片属性：旋转的角度
     *
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree = -1;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                default:
                    degree = 0;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
            degree = -1;
        }
        return degree;
    }

    /***
     * 旋转图片
     * @param angle
     * @param bitmap
     * @return
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    /**
     * 将图片转换成Base64编码的字符串
     */
    public static String imageToBase64(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try {
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }


    /**
     * 文件转base64字符串
     *
     * @param file
     * @return
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return base64;
    }

    /**
     * 缩放Drawable
     *
     * @drawable 原来的Drawable
     * @w 指定的宽
     * @h 指定的高
     */
    public static Drawable zoomDrawable(Drawable drawable, int w, int h, Context mContext, DisplayMetrics metrics) {
        //获取原来Drawable的宽高
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        //将Drawable转换成Bitmap
        Bitmap oldbmp = drawableToBitmap(drawable);
        //计算scale
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);
        //生成新的Bitmap
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
        //设置bitmap转成drawable后尺寸不变
        //这个很关键后面解释！！

        Resources resources = new Resources(mContext.getAssets(), metrics, null);
        return new BitmapDrawable(resources, newbmp);
    }

    /**
     * 将Drawable转换为Bitmap
     *
     * @param drawable
     * @return
     */
    private static Bitmap drawableToBitmap(Drawable drawable) {
        //取drawable的宽高
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        //取drawable的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
                ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        //创建对应的bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);
        //创建对应的bitmap的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        //把drawable内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }


    public static String getNetFileSizeDescription(long size) {
        StringBuffer bytes = new StringBuffer();
        DecimalFormat format = new DecimalFormat("###.0");
        if (size >= 1024 * 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0 * 1024.0));
            bytes.append(format.format(i)).append("GB");
        } else if (size >= 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0));
            bytes.append(format.format(i)).append("MB");
        } else if (size >= 1024) {
            double i = (size / (1024.0));
            bytes.append(format.format(i)).append("KB");
        } else if (size < 1024) {
            if (size <= 0) {
                bytes.append("0B");
            } else {
                bytes.append((int) size).append("B");
            }
        }
        return bytes.toString();
    }

    public static Bitmap readGrayBitMap(Context context, int resId) {
        Bitmap bitmap = readBitmap(context, resId);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap grayImg = null;
        try {

            grayImg = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(grayImg);
            Paint paint = new Paint();
            ColorMatrix colorMatrix = new ColorMatrix();//仰仗这玩意了
            colorMatrix.setSaturation(0);
            ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
                    colorMatrix);
            paint.setColorFilter(colorMatrixFilter);
            canvas.drawBitmap(bitmap, 0, 0, paint);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return grayImg;
    }

    public static Bitmap readBitmap(Context context, int id) {

        BitmapFactory.Options opt = new BitmapFactory.Options();

        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        opt.inInputShareable = true;
        opt.inPurgeable = true;
        InputStream is = context.getResources().openRawResource(id);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    /**
     * 保存bitmap图片到本地
     */
    public static void saveBitmapToLocal(Context mContext, Bitmap bitmap) {


        // 先将图片保存到文件
        File imageDir = new File(Environment.getExternalStorageDirectory(), "xcyh");
        if (!imageDir.exists()) {
            imageDir.mkdir();
        }

        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String   filename = "xcyh"+sdf.format(date)+".jpg";
        File file = new File(imageDir, filename);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            MyToast.show("保存成功");
        } catch (IOException e) {
            e.printStackTrace();
            MyToast.show("保存失败");
        }
        // 再通知图库更新数据库
        Uri uri = Uri.fromFile(file);
        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
    }
}
