package com.zhaohaoting.framework.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.telephony.mbms.FileInfo;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.application.ZhtApplication;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * author：Created by zhaohaoting on 2018/11/30
 * email：526309416@qq.com
 * desc：
 */
public class FileTypeUtils {
    public static final FileFilter ALL_FOLDER_AND_FILES_FILTER = new FileFilter() {
        public boolean accept(File pathname) {
            return !pathname.isHidden();
        }
    };
    public static final int KILOBYTE = 1024;
    public static final int MEGABYTE = 1048576;
    public static final int GIGABYTE = 1073741824;

    public FileTypeUtils() {
    }

    public static int fileTypeImageId(String fileName) {
        int id;
        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_image_file_suffix))) {
            id = R.drawable.rc_file_icon_picture;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_file_file_suffix))) {
            id = R.drawable.rc_file_icon_file;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_video_file_suffix))) {
            id = R.drawable.rc_file_icon_video;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_audio_file_suffix))) {
            id = R.drawable.rc_file_icon_audio;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_word_file_suffix))) {
            id = R.drawable.rc_file_icon_word;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_excel_file_suffix))) {
            id = R.drawable.rc_file_icon_excel;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_ppt_file_suffix))) {
            id = R.drawable.rc_file_icon_ppt;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_pdf_file_suffix))) {
            id = R.drawable.rc_file_icon_pdf;
        } else {
            id = R.drawable.rc_file_icon_else;
        }

        return id;
    }

    public static Intent getOpenFileIntent(String fileName, String fileSavePath) {
        Intent intent = new Intent("android.intent.action.VIEW");
        String type = null;
        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_image_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "image/*";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_file_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "text/plain";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_video_file_suffix))) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("oneshot", 0);
            intent.putExtra("configchange", 0);
            type = "video/*";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_audio_file_suffix))) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("oneshot", 0);
            intent.putExtra("configchange", 0);
            type = "audio/*";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_word_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "application/msword";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_excel_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "application/vnd.ms-excel";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_pdf_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "application/pdf";
        }

        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_ppt_file_suffix))) {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            type = "application/vnd.ms-powerpoint";
        }

        if (type != null && fileSavePath != null && isIntentHandlerAvailable(ZhtApplication.getInstance(), intent)) {
            Uri uri = FileProvider.getUriForFile(ZhtApplication.getInstance(), ZhtApplication.getInstance().getApplicationContext().getPackageName() + ".provider", new File(fileSavePath));
            intent.setDataAndType(uri, type);
            return intent;
        } else {
            return null;
        }
    }

    private static boolean checkSuffix(String fileName, String[] fileSuffix) {
        String[] var2 = fileSuffix;
        int var3 = fileSuffix.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String suffix = var2[var4];
            if (fileName != null && fileName.toLowerCase().endsWith(suffix)) {
                return true;
            }
        }

        return false;
    }

    private static boolean isIntentHandlerAvailable(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        @SuppressLint("WrongConstant") List<ResolveInfo> infoList = packageManager.queryIntentActivities(intent, Intent.FLAG_ACTIVITY_NO_ANIMATION);
        return infoList.size() > 0;
    }

    public static List<FileInfo> getTextFilesInfo(File fileDir) {
        List<FileInfo> textFilesInfo = new ArrayList();
        FileFilter fileFilter = new FileTypeUtils.FileTypeFilter(ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_file_file_suffix));
        getFileInfos(fileDir, fileFilter, textFilesInfo);
        return textFilesInfo;
    }

    private static void getFileInfos(File fileDir, FileFilter fileFilter, List<FileInfo> fileInfos) {
        File[] listFiles = fileDir.listFiles(fileFilter);
        if (listFiles != null) {
            File[] var4 = listFiles;
            int var5 = listFiles.length;

            for (int var6 = 0; var6 < var5; ++var6) {
                File file = var4[var6];
                if (file.isDirectory()) {
                    getFileInfos(file, fileFilter, fileInfos);
                } else if (file.length() != 0L) {
                    FileInfo fileInfo = getFileInfoFromFile(file);
                    fileInfos.add(fileInfo);
                }
            }
        }

    }

    public static int getNumFilesInFolder(FileInfo fileInfo) {
        if (!fileInfo.isDirectory()) {
            return 0;
        } else {
            File[] files = (new File(fileInfo.getFilePath())).listFiles(ALL_FOLDER_AND_FILES_FILTER);
            return files == null ? 0 : files.length;
        }
    }

    private static int getFileTypeImageId(String fileName) {
        int id;
        if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_file_file_suffix))) {
            id = R.drawable.rc_ad_list_file_icon;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_video_file_suffix))) {
            id = R.drawable.rc_ad_list_video_icon;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_audio_file_suffix))) {
            id = R.drawable.rc_ad_list_audio_icon;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_ppt_file_suffix))) {
            id = R.drawable.rc_ad_list_ppt_icon;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_pdf_file_suffix))) {
            id = R.drawable.rc_ad_list_pdf_icon;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_image_file_suffix))) {
            id = R.drawable.rc_file_icon_picture;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_word_file_suffix))) {
            id = R.drawable.rc_file_icon_word;
        } else if (checkSuffix(fileName, ZhtApplication.getInstance().getResources().getStringArray(R.array.rc_excel_file_suffix))) {
            id = R.drawable.rc_file_icon_excel;
        } else {
            id = R.drawable.rc_ad_list_other_icon;
        }

        return id;
    }

    public static int getFileIconResource(FileInfo file) {
        return file.isDirectory() ? R.drawable.rc_ad_list_folder_icon : getFileTypeImageId(file.getFileName());
    }

    private static FileInfo getFileInfoFromFile(File file) {
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileName(file.getName());
        fileInfo.setFilePath(file.getPath());
        fileInfo.setDirectory(file.isDirectory());
        if (file.isDirectory()) {
            fileInfo.setFileSize((long) getNumFilesInFolder(fileInfo));
        } else {
            fileInfo.setFileSize(file.length());
        }

        int lastDotIndex = file.getName().lastIndexOf(".");
        if (lastDotIndex > 0) {
            String fileSuffix = file.getName().substring(lastDotIndex + 1);
            fileInfo.setSuffix(fileSuffix);
        }

        return fileInfo;
    }

    public static class FileInfo implements Serializable {
        private static final long serialVersionUID = -4830812821556630987L;
        String fileName;
        String filePath;
        long fileSize;
        boolean isDirectory;
        String suffix;

        public boolean isDirectory() {
            return this.isDirectory;
        }

        public String getSuffix() {
            return this.suffix;
        }

        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }

        public void setDirectory(boolean directory) {
            this.isDirectory = directory;
        }

        public String getFileName() {
            return this.fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getFilePath() {
            return this.filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public long getFileSize() {
            return this.fileSize;
        }

        public void setFileSize(long fileSize) {
            this.fileSize = fileSize;
        }

        public FileInfo() {
        }
    }

    public static final class FileTypeFilter implements FileFilter {
        private String[] filesSuffix;

        public FileTypeFilter(String[] fileSuffix) {
            this.filesSuffix = fileSuffix;
        }

        public boolean accept(File pathname) {
            return !pathname.isHidden() && (pathname.isDirectory() || FileTypeUtils.checkSuffix(pathname.getName(), this.filesSuffix));
        }
    }
}
