package com.bc_chat.im.messages.gif;

import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import io.rong.common.RLog;

public class FileUtils {
    private static String TAG = "FileUtils";

    public static boolean isValidateLocalUri(Uri pUri) {
        return uriStartWithFile(pUri) || uriStartWithContent(pUri);
    }

    public static boolean uriStartWithContent(Uri srcUri) {
        return srcUri != null && "content".equals(srcUri.getScheme());
    }


    public static boolean uriStartWithFile(Uri pUri) {
        return pUri != null && "file".equals(pUri.getScheme()) && pUri.toString().length() > 7;
    }

    public static boolean copyFileToInternal(Context context, Uri srcUri, String desPath, String name) {
        if (uriStartWithFile(srcUri)) {
            return copyFile(srcUri.toString(), desPath, name);
        } else {
            return uriStartWithContent(srcUri) ? copyFile(context, srcUri, (new File(desPath, name)).getAbsolutePath()) : false;
        }
    }

    public static boolean copyFile(String srcPath, String path, String name) {
        if (TextUtils.isEmpty(srcPath)) {
            RLog.e(TAG, "copyFile src should not be null!");
            return false;
        } else {
            File src = new File(srcPath);
            if (!src.exists()) {
                RLog.e(TAG, "copyFile: src file does not exist! -" + src.getAbsolutePath());
                return false;
            } else {
                File dest = new File(path);
                if (!dest.exists()) {
                    RLog.d(TAG, "copyFile: dir does not exist!");
                    boolean successMkdir = dest.mkdirs();
                    if (!successMkdir) {
                        RLog.e(TAG, "Created folders unSuccessfully");
                    }
                }

                dest = new File(path, name);
                FileInputStream fis = null;
                FileOutputStream fos = null;

                boolean var8;
                try {
                    fis = new FileInputStream(src);
                    fos = new FileOutputStream(dest);
                    byte[] buffer = new byte[1024];

                    int length;
                    while ((length = fis.read(buffer)) != -1) {
                        fos.write(buffer, 0, length);
                    }

                    fos.flush();
                    return true;
                } catch (IOException var22) {
                    RLog.e(TAG, "copyFile: Exception!", var22);
                    var8 = false;
                } finally {
                    try {
                        if (fos != null) {
                            fos.close();
                        }
                    } catch (IOException var21) {
                        RLog.e(TAG, "copyFile fos close", var21);
                    }

                    try {
                        if (fis != null) {
                            fis.close();
                        }
                    } catch (IOException var20) {
                        RLog.e(TAG, "copyFile fis close", var20);
                    }

                }

                return var8;
            }
        }
    }

    public static boolean copyFile(Context context, Uri srcUri, String desPath) {
        if (!uriStartWithContent(srcUri)) {
            return false;
        } else {
            ParcelFileDescriptor pfd;
            try {
                pfd = context.getContentResolver().openFileDescriptor(srcUri, "r");
            } catch (FileNotFoundException var22) {
                RLog.e(TAG, "copyFile srcUri is error uri is " + srcUri.toString());
                return false;
            }

            if (pfd != null) {
                FileInputStream fis = null;
                FileOutputStream fos = null;

                boolean var7;
                try {
                    fis = new FileInputStream(pfd.getFileDescriptor());
                    fos = new FileOutputStream(desPath);
                    byte[] buffer = new byte[1024];

                    int length;
                    while ((length = fis.read(buffer)) != -1) {
                        fos.write(buffer, 0, length);
                    }

                    fos.flush();
                    return true;
                } catch (IOException var23) {
                    RLog.e(TAG, "copyFile: Exception!", var23);
                    var7 = false;
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException var21) {
                            RLog.e(TAG, "copyFile: Exception!", var21);
                        }
                    }

                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException var20) {
                            RLog.e(TAG, "copyFile: Exception!", var20);
                        }
                    }

                }

                return var7;
            } else {
                return true;
            }
        }
    }

    public static FileInfo getFileInfoByUri(Context context, Uri uri) {
        if (uriStartWithContent(uri)) {
            return getFileInfoByContent(context, uri);
        } else {
            return uriStartWithFile(uri) ? getFileInfoByFile(uri) : null;
        }
    }

    private static FileInfo getFileInfoByContent(Context context, Uri uri) {
//        if (uriStartWithContent(uri)) {
//            String[] projection = new String[]{"_display_name", "_size", "mime_type"};
//            Cursor cursor = null;
//
//            FileInfo var15;
//            try {
//                String name;
//                try {
//                    FileInfo fileInfo = new FileInfo();
//                    cursor = context.getContentResolver().query(uri, projection, (String) null, (String[]) null, (String) null);
//                    if (cursor == null) {
//                        RLog.e(TAG, "getFileInfoByContent cursor is null");
//                        name = null;
//                        return null;
//                    }
//
//                    if (!cursor.moveToFirst()) {
//                        return null;
//                    }
//
//                    name = cursor.getString(0);
//                    long size = cursor.getLong(1);
//                    fileInfo.setName(name);
//                    fileInfo.setSize(size);
//                    if (TextUtils.isEmpty(name)) {
//                        RLog.e(TAG, "getFileInfoByContent getName is empty");
//                        var15 = null;
//                        return var15;
//                    }
//
//                    int lastDotIndex = name.lastIndexOf(".");
//                    if (lastDotIndex > 0) {
//                        String fileSuffix = name.substring(lastDotIndex + 1);
//                        fileInfo.setType(fileSuffix);
//                    }
//
//                    if (cursor.moveToNext()) {
//                        RLog.e(TAG, "uri is error,cursor has second value,uri is" + uri);
//                    }
//
//                    var15 = fileInfo;
//                } catch (Exception var13) {
//                    RLog.e(TAG, "getDocumentByContent is error", var13);
//                    name = null;
//                    return null;
//                }
//            } finally {
//                if (cursor != null) {
//                    cursor.close();
//                }
//
//            }
//
//            return var15;
//        } else {
//            RLog.e(TAG, "getDocumentByContent uri is not content");
//            return null;
//        }
        return null;
    }

    private static FileInfo getFileInfoByFile(Uri uri) {
//        if (uriStartWithFile(uri)) {
//            String filePath = uri.toString().substring(7);
//            File file = new File(filePath);
//            if (file.exists()) {
//                FileInfo fileInfo = new FileInfo();
//                String name = file.getName();
//                fileInfo.setSize(file.length());
//                fileInfo.setName(name);
//                int lastDotIndex = name.lastIndexOf(".");
//                if (lastDotIndex > 0) {
//                    String fileSuffix = file.getName().substring(lastDotIndex + 1);
//                    fileInfo.setType(fileSuffix);
//                }

//                return fileInfo;
//            } else {
//                return null;
//            }
//        } else {
//            RLog.e(TAG, "getDocumentByFile uri is not file");
//            return null;
//        }
        return null;

    }

}
