package com.codekidlabs.storagechooser.adapters;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
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.ProgressBar;
import android.widget.TextView;

import com.codekidlabs.storagechooser.R;
import com.codekidlabs.storagechooser.StorageChooser;
import com.codekidlabs.storagechooser.StorageChooserView;
import com.codekidlabs.storagechooser.animators.MemorybarAnimation;
import com.codekidlabs.storagechooser.exceptions.MemoryNotAccessibleException;
import com.codekidlabs.storagechooser.models.Storages;
import com.codekidlabs.storagechooser.utils.MemoryUtil;

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

public class StorageChooserListAdapter extends BaseAdapter {

    private List<Storages> storagesList;
    private Context mContext;
    private boolean shouldShowMemoryBar;

    private ProgressBar memoryBar;
    private static int memoryPercentile;


    public StorageChooserListAdapter(List<Storages> 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_storage, viewGroup, false);

        //for animation set current position to provide animation delay


        TextView storageName = (TextView) rootView.findViewById(R.id.storage_name);
        TextView memoryStatus = (TextView) rootView.findViewById(R.id.memory_status);
        memoryBar = (ProgressBar) rootView.findViewById(R.id.memory_bar);

        Storages storages = storagesList.get(i);
        final SpannableStringBuilder str = new SpannableStringBuilder(storages.getStorageTitle() + " (" + storages.getMemoryTotalSize() + ")");

        str.setSpan(new StyleSpan(Typeface.ITALIC), getSpannableIndex(str), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        String availableText = mContext.getString(R.string.text_freespace, storages.getMemoryAvailableSize());
        storageName.setText(str);
        memoryStatus.setText(availableText);

        memoryStatus.setTextColor(ContextCompat.getColor(mContext, R.color.memory_status_color));
        DrawableCompat.setTint(memoryBar.getProgressDrawable(), ContextCompat.getColor(mContext, R.color.memory_bar_color));

        try {
            memoryPercentile = getPercentile(storages.getStoragePath());
        } catch (MemoryNotAccessibleException e) {
            e.printStackTrace();
        }
        // THE ONE AND ONLY MEMORY BAR
        if(shouldShowMemoryBar) {
            memoryBar.setMax(100);
            memoryBar.setProgress(memoryPercentile);
        } else if(memoryPercentile == 0) {
            memoryBar.setVisibility(View.GONE);
        } else {
            memoryBar.setVisibility(View.GONE);
        }

        runMemorybarAnimation(i);
        return rootView;

    }

    private void runMemorybarAnimation(int pos) {
        MemorybarAnimation animation = new MemorybarAnimation(memoryBar,0, memoryPercentile);
        animation.setDuration(500);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());

        if(pos > 0) {
            animation.setStartOffset(300);
        }

        memoryBar.startAnimation(animation);
    }

    /**
     * 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;
    }

    /**
     * calculate percentage of memory left for memorybar
     * @param path use same statfs
     * @return integer value of the percentage with amount of storage used
     */
    private int getPercentile(String path) throws MemoryNotAccessibleException {
        MemoryUtil memoryUtil = new MemoryUtil();
        int percent;

        int availableMem = (int) memoryUtil.getAvailableMemorySize(path);
        int totalMem = (int) memoryUtil.getTotalMemorySize(path);

        if(totalMem > 0) {
            percent = ((availableMem * 100) / totalMem);
            return 100 - percent;
        } else {
            throw new MemoryNotAccessibleException("Cannot compute memory for " + path);
        }
    }

    /**
     * remove KiB,MiB,GiB text that we got from MemoryUtil.getAvailableMemorySize() &
     * MemoryUtil.getTotalMemorySize()
     * @param size String in the format of user readable string, with MB, GiB .. suffix
     * @return integer value of the percentage with amount of storage used
     */
    private long getMemoryFromString(String size) {
        long mem = 0;

        if(size.contains("MiB")) {
            mem = Integer.parseInt(size.replace(",","").replace("MiB",""));
        } else if (size.contains("GiB")){
            mem = Integer.parseInt(size.replace(",","").replace("GiB",""));
        } else {
            mem = Integer.parseInt(size.replace(",","").replace("KiB",""));
        }


        Log.d("TAG", "Memory:"+ mem);
        return mem;
    }
}

@@ -2355,17 +2355,16 @@
  delay%0A%0A
-%0A
         
@@ -3578,32 +3578,60 @@
 uldShowMemoryBar
+ && memoryPercentile != null
 ) %7B%0A            
@@ -3718,88 +3718,35 @@
     
-%7D else if(memoryPercentile == 0) %7B%0A            memoryBar.setVisibility(View.GONE
+    runMemorybarAnimation(i
 );%0A 
@@ -3819,25 +3819,24 @@
       %7D%0A
-%0A
         
 runMemor
@@ -3831,33 +3831,8 @@
     
-runMemorybarAnimation(i);
 %0A   
@@ -5055,16 +5055,22 @@
 ercent =
+ 100 -
  ((avail
@@ -5101,42 +5101,8 @@
 m);%0A
-            return 100 - percent;%0A
     
@@ -5209,24 +5209,57 @@
 ;%0A        %7D%0A
+        %0A        return percent;%0A
     %7D%0A%0A    /
@@ -5946,32 +5946,32 @@
 ace(%22KiB%22,%22%22));%0A
+
         %7D%0A%0A%0A    
@@ -5965,17 +5965,16 @@
      %7D%0A%0A
-%0A
         

Fix check for initialization of memoryPercentile