package com.codekidlabs.storagechooser.adapters;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.codekidlabs.storagechooser.R;
import com.codekidlabs.storagechooser.animators.MemorybarAnimation;
import com.codekidlabs.storagechooser.models.Storages;
import com.codekidlabs.storagechooser.utils.FileUtil;
import com.codekidlabs.storagechooser.utils.ThumbnailUtil;

import java.io.File;
import java.util.List;

public class FilePickerAdapter extends BaseAdapter {

    private List<String> storagesList;
    private Context mContext;
    private boolean shouldShowMemoryBar;
    public static boolean shouldEnable = true;

    public String prefixPath;


    public FilePickerAdapter(List<String> storagesList, Context mContext, boolean shouldShowMemoryBar) {
        this.storagesList = storagesList;
        this.mContext = mContext;
        this.shouldShowMemoryBar = shouldShowMemoryBar;
    }

    @Override
    public int getCount() {
        return storagesList.size();
    }

    @Override
    public Object getItem(int i) {
        return storagesList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rootView = inflater.inflate(R.layout.row_custom_paths, viewGroup, false);

        ImageView pathFolderIcon = (ImageView) rootView.findViewById(R.id.path_folder_icon);
        if(FileUtil.isDir(prefixPath + "/" + storagesList.get(i))) {
            applyFolderTint(pathFolderIcon);
        }

        ThumbnailUtil thumbnailUtil = new ThumbnailUtil(mContext);
        thumbnailUtil.init(pathFolderIcon, storagesList.get(i));

        TextView storageName = (TextView) rootView.findViewById(R.id.storage_name);
        storageName.setText(storagesList.get(i));

        Log.e("SC_FA", prefixPath + "/" + storagesList.get(i));

        return rootView;

    }


    /**
     * return the spannable index of character '('
     * @param str SpannableStringBuilder to apply typeface changes
     * @return index of '('
     */
    private int getSpannableIndex(SpannableStringBuilder str) {
        return str.toString().indexOf("(") + 1;
    }

    public void setPrefixPath(String path) {
        this.prefixPath = path;
    }

    public String getPrefixPath() {
        return prefixPath;
    }

    private void applyFolderTint(ImageView im) {
        im.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary));
    }

    @Override
    public boolean isEnabled(int position) {
        return shouldEnable;
    }
}

@@ -1116,16 +1116,57 @@
 ixPath;%0A
+    private ThumbnailUtil thumbnailUtil;%0A
 %0A%0A    pu
@@ -1391,24 +1391,110 @@
 owMemoryBar;
+%0A%0A        // create instance once%0A        thumbnailUtil = new ThumbnailUtil(mContext);
 %0A    %7D%0A%0A    
@@ -2241,75 +2241,8 @@
  %7D%0A%0A
-        ThumbnailUtil thumbnailUtil = new ThumbnailUtil(mContext);%0A
     
@@ -2442,73 +2442,8 @@
 );%0A%0A
-        Log.e(%22SC_FA%22, prefixPath + %22/%22 + storagesList.get(i));%0A%0A
     

create thumbnailUtil instance once