package de.beyondsoft.ownchat.filepicker;

import java.util.ArrayList;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.filepicker.models.BaseFile;
import de.beyondsoft.ownchat.filepicker.models.FileType;
import de.beyondsoft.ownchat.filepicker.utils.Orientation;

public class PickerManager {
    private static PickerManager mInstance = new PickerManager();
    private int mMaxCount = FilePickerConst.DEFAULT_MAX_COUNT;
    private int mCurrentCount;
    private boolean mShowImages = true;
    private int mCameraDrawable = R.drawable.orca_attach_camera_normal;

    public static PickerManager getInstance() {
        return mInstance;
    }

    private ArrayList<String> mediaFiles;
    private ArrayList<String> docFiles;

    private ArrayList<FileType> fileTypes;

    private int theme = R.style.AppTheme;

    private boolean showVideos;

    private boolean showGif;

    private boolean docSupport = true;

    private boolean enableCamera = true;

    private Orientation orientation = Orientation.UNSPECIFIED;

    private boolean showFolderView = true;

    private String providerAuthorities;

    private PickerManager() {
        mediaFiles = new ArrayList<>();
        docFiles = new ArrayList<>();
        fileTypes = new ArrayList<>();
    }

    void setMaxCount(int count) {
        clearSelections();
        this.mMaxCount = count;
    }

    public int getMaxCount() {
        return mMaxCount;
    }

    int getCurrentCount() {
        return mCurrentCount;
    }

    public void add(String path, int type) {
        if(path != null && shouldAdd()) {
            if(!mediaFiles.contains(path) && type == FilePickerConst.FILE_TYPE_MEDIA) {
                mediaFiles.add(path);
            } else if(type == FilePickerConst.FILE_TYPE_DOCUMENT) {
                docFiles.add(path);
            } else {
                return;
            }

            mCurrentCount++;
        }
    }

    public void add(ArrayList<String> paths, int type) {
        for(int index = 0; index < paths.size(); index++) {
            add(paths.get(index), type);
        }
    }

    public void remove(String path, int type) {
        if((type == FilePickerConst.FILE_TYPE_MEDIA) && mediaFiles.contains(path)) {
            mediaFiles.remove(path);
            mCurrentCount--;
        } else if(type == FilePickerConst.FILE_TYPE_DOCUMENT) {
            docFiles.remove(path);

            mCurrentCount--;
        }
    }

    public boolean shouldAdd() {
        return mCurrentCount < mMaxCount;
    }

    public ArrayList<String> getSelectedPhotos() {
        return mediaFiles;
    }

    public ArrayList<String> getSelectedFiles() {
        return docFiles;
    }

    public ArrayList<String> getSelectedFilePaths(ArrayList<BaseFile> files) {
        ArrayList<String> paths = new ArrayList<>();

        for(int index = 0; index < files.size(); index++) {
            paths.add(files.get(index).getPath());
        }

        return paths;
    }

    private void clearSelections() {
        docFiles.clear();
        mediaFiles.clear();
        fileTypes.clear();
        mCurrentCount = 0;
        mMaxCount = 0;
    }

    public int getTheme() {
        return theme;
    }

    void setTheme(int theme) {
        this.theme = theme;
    }

    public boolean showVideo() {
        return showVideos;
    }

    void setShowVideos(boolean showVideos) {
        this.showVideos = showVideos;
    }

    public boolean showImages() {
        return mShowImages;
    }

    void setShowImages(boolean showImages) {
        this.mShowImages = showImages;
    }

    public boolean isShowGif() {
        return showGif;
    }

    void setShowGif(boolean showGif) {
        this.showGif = showGif;
    }

    public boolean isShowFolderView() {
        return showFolderView;
    }

    void setShowFolderView(boolean showFolderView) {
        this.showFolderView = showFolderView;
    }

    void addFileType(FileType fileType) {
        fileTypes.add(fileType);
    }

    void addDocTypes() {
        String[] pdfs = {"pdf"};
        fileTypes.add(new FileType(FilePickerConst.PDF, pdfs, R.drawable.document));

        String[] docs = {"doc", "docx", "dot", "dotx"};
        fileTypes.add(new FileType(FilePickerConst.DOC, docs, R.drawable.document));

        String[] ppts = {"ppt", "pptx"};
        fileTypes.add(new FileType(FilePickerConst.PPT, ppts, R.drawable.document));

        String[] xlss = {"xls", "xlsx"};
        fileTypes.add(new FileType(FilePickerConst.XLS, xlss, R.drawable.document));

        String[] txts = {"txt"};
        fileTypes.add(new FileType(FilePickerConst.TXT, txts, R.drawable.document));

        String[] ods = {"ods", "odt", "odp", "odc", "odf", "odg", "otp", "ots", "ott"};
        fileTypes.add(new FileType(FilePickerConst.ODS, ods, R.drawable.document));
    }

    public ArrayList<FileType> getFileTypes() {
        return fileTypes;
    }

    boolean isDocSupport() {
        return docSupport;
    }

    void setDocSupport(boolean docSupport) {
        this.docSupport = docSupport;
    }

    public boolean isEnableCamera() {
        return enableCamera;
    }

    void setEnableCamera(boolean enableCamera) {
        this.enableCamera = enableCamera;
    }

    public Orientation getOrientation() {
        return orientation;
    }

    public void setOrientation(Orientation orientation) {
        this.orientation = orientation;
    }

    public String getProviderAuthorities() {
        return providerAuthorities;
    }

    void setProviderAuthorities(String providerAuthorities) {
        this.providerAuthorities = providerAuthorities;
    }

    void setCameraDrawable(int drawable) {
        this.mCameraDrawable = drawable;
    }

    public int getCameraDrawable() {
        return mCameraDrawable;
    }
}
