package oko.tm.oko_pro_classic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static oko.tm.oko_pro_classic.R.string.event_log;


public class EventLog extends AppCompatActivity {
    ListView listview;

    String object_setting="";//MainActivity.APP_PREFERENCES;
    String object_name="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_log);
        Intent intent = getIntent();
        //object setting
        object_setting = intent.getStringExtra(StartActivity.OBJECT_SETTING);
        object_name = intent.getStringExtra(StartActivity.OBJECT_NAME);
        setTitle(event_log); //getActivity().
        String title= getTitle().toString() + ", " + object_name;
        setTitle(title);

        try {
        listview = (ListView) findViewById(R.id.listView_event_log);


        final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(loadArray(MainActivity.EVENTS_ARRAY,this)));


        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (EventLog.this, android.R.layout.simple_list_item_1, ListElementsArrayList);


        listview.setAdapter(adapter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() //onResume()
    {
        // TODO Auto-generated method stub
        super.onResume(); //onResume();
    }

    @Override
    protected void onPause() {


        // TODO Auto-generated method stub
        super.onPause();
    }

    public String[] loadArray(String arrayName, Context mContext) {

        String array[] = new String[MainActivity.MAX_EVENTS];

        try {
            SharedPreferences prefs = mContext.getSharedPreferences(object_setting, Context.MODE_PRIVATE);
            int event_pointer = prefs.getInt(arrayName + "_pointer", 0);

            int cur_event=1;

            if (event_pointer>0) {
                for (int i = event_pointer - 1; i >= 0; i--) {
                    array[cur_event - 1] = String.valueOf(cur_event).concat("  ") + prefs.getString(arrayName + "_" + String.valueOf(i), "");
                    cur_event++;
                }
            }

            for (int i = MainActivity.MAX_EVENTS-1; i >= event_pointer; i--) {
                array[cur_event-1] = String.valueOf(cur_event).concat("  ") + prefs.getString(arrayName + "_" + String.valueOf(i), "");
                cur_event++;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;

    }
}
