package com.school.denis_niko.projektjanez;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Niko
 */

public class StatsActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stats);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String readData = readFile("stats.txt");
        int totalTime = getTotalTime(readData);
        float avgTime = getAvgTime(readData);

        TextView text = (TextView) findViewById(R.id.totalTimeValue);
        text.setText(String.format(getString(R.string.total_time_display), totalTime));

        text = (TextView) findViewById(R.id.avgTimeValue);
        text.setText(String.format(getString(R.string.avg_time_display), avgTime));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.stats_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(deleteFile("stats.txt"))
            Toast.makeText(this, "Stats reset", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "Operation failed", Toast.LENGTH_SHORT).show();

        return id == R.id.stats_settings || super.onOptionsItemSelected(item);
    }

    public boolean deleteFile(String fileName) {
        File dir = getFilesDir();
        File file = new File(dir, fileName);
        return file.delete();
    }

    private String readFile(String fileName) {
        String readString = "";

        try {
            InputStream inputStream = openFileInput(fileName);

            if(inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuilder stringBuilder = new StringBuilder();

                while ((readString = bufferedReader.readLine()) != null)
                    stringBuilder.append(readString);

                inputStream.close();
                readString = stringBuilder.toString();
            }
        } catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("login activity", "Cannot read file: " + e.toString());
        }

        return readString;
    }

    private int getTotalTime(String data) {
        // gets total time the timer has run for
        char[] times = data.toCharArray();
        int totalTime = 0;

        for(int i = 0; i < times.length; i++) {
            if(times[i] == '/') {
                int j = i+1;
                String temp = "";
                while(times[j] != ' ') {
                    temp += String.valueOf(times[j]);
                    j++;
                }
                i = j;
                totalTime += Integer.parseInt(temp);
            }
        }

        return totalTime;
    }

    private float getAvgTime(String data) {
        // gets the total avg time
        int numberOfData = 0;
        float avgTime;

        for(int i = 0; i < data.length(); i++) {
            if(data.charAt(i) == '/')
                numberOfData++;
        }

        if(numberOfData == 0)
            avgTime = 0;
        else
            avgTime = (float)getTotalTime(data)/numberOfData;

        return avgTime;
    }

}

@@ -878,16 +878,17 @@
 .txt%22);%0A
+%0A
         
@@ -883,32 +883,78 @@
 );%0A%0A        int 
+dataAmount = getDataAmount(readData),%0A        
 totalTime = getT
@@ -1008,24 +1008,37 @@
 AvgTime(
-readData
+totalTime, dataAmount
 );%0A%0A    
@@ -3025,25 +3025,26 @@
  int get
-TotalTime
+DataAmount
 (String 
@@ -3063,91 +3063,234 @@
     
-// gets total time the timer has run for%0A        char%5B%5D times = data.toCharArray();
+int dataAmount = 0;%0A%0A        for(int i = 0; i %3C data.length(); i++) %7B%0A            if(data.charAt(i) == '/')%0A                dataAmount++;%0A        %7D%0A%0A        return dataAmount;%0A    %7D%0A%0A    private int getTotalTime(String data) %7B
 %0A   
@@ -3308,24 +3308,49 @@
 talTime = 0;
+%0A        String tmp = %22%22;
 %0A%0A        fo
@@ -3370,20 +3370,21 @@
 i %3C 
-times
+data
 .length
+()
 ; i+
@@ -3406,17 +3406,24 @@
   if
-(times%5Bi%5D
+ (data.charAt(i)
  == 
@@ -3450,52 +3450,10 @@
    i
-nt j = i+1;%0A                String temp = %22%22
+++
 ;%0A  
@@ -3472,24 +3472,30 @@
   while(
-times%5Bj%5D
+data.charAt(i)
  != ' ')
@@ -3522,38 +3522,27 @@
    t
-e
 mp += 
-String.valueOf(times%5Bj%5D
+data.charAt(i
 );%0A 
@@ -3560,17 +3560,17 @@
         
-j
+i
 ++;%0A    
@@ -3587,30 +3587,8 @@
   %7D%0A
-                i = j;
 %0A   
@@ -3635,14 +3635,39 @@
 nt(t
-e
 mp);%0A
+                tmp = %22%22;%0A
     
@@ -3753,157 +3753,37 @@
 ime(
-String data) %7B%0A        // gets the total avg time%0A        int numberOfData = 0;%0A        float avgTime;%0A%0A        for(int i = 0; i %3C data.length(); i++
+int totalTime, int dataAmount
 ) %7B%0A
@@ -3794,234 +3794,113 @@
     
- 
-   
 if
+ 
 (data
-.charAt(i) == '/')%0A                numberOfData++;%0A        %7D%0A%0A        if(numberOfData == 0)%0A            avgTime = 0;%0A        else%0A            avgTime = (float)getTotalTime(data)/numberOfData;%0A%0A        return avgTime
+Amount != 0)%0A            return (float) totalTime / dataAmount;%0A        else%0A            return 0
 ;%0A  

Clean up stats code