package com.pot.timerly;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private Handler mRecordingHandler;
    private int mCurrentTime = 0;
    private final int UPDATE_INTERVAL = 100; // 100ms
    private boolean mIsRecording = false;
    private TextView mRecoringText; // Correspond to the timer text

    // Runable (executable code run every x ms) which update the timer
    Runnable mRecordingRunable = new Runnable() {
        @Override
        public void run() {
            // Update the GUI:
            // TODO: Format as date
            mCurrentTime += UPDATE_INTERVAL;
            mRecoringText.setText(Integer.toString(mCurrentTime));

            mRecordingHandler.postDelayed(mRecordingRunable, UPDATE_INTERVAL);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Loading dataset (Here ???)

        // TODO: Load and use real dataset!
        String[] myDataset = {
                "String_1",
                "Blezffdgdfg",
                "Test 43"
        };

        // Set up the timer
        mRecordingHandler = new Handler();

        // Configure the recycler view

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        // For improving performance (static size)
        mRecyclerView.setHasFixedSize(true);
        // Linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        // Specify the adapter
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);

        // Configure other GUI elements

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mRecoringText = (TextView) findViewById(R.id.recording_text);
        mRecoringText.setText("Inactive");

        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!mIsRecording) {
                    // Invert the fab icon
                    // TODO: Add transition icon animation
                    fab.setImageResource(android.R.drawable.ic_media_pause);

                    // Start the recording
                    mCurrentTime = 0;
                    mRecordingRunable.run();
                } else {
                    // Invert the fab icon
                    fab.setImageResource(android.R.drawable.ic_media_play);

                    // Stop the recording
                    mRecordingHandler.removeCallbacks(mRecordingRunable);

                    // Save the recording
                }
                mIsRecording = !mIsRecording;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -131,16 +131,63 @@
 Button;%0A
+import android.support.design.widget.Snackbar;%0A
 import a
@@ -747,24 +747,102 @@
 entTime = 0;
+ // TODO: Use clock system time %3E more precise (instead of interval increment)
 %0A    private
@@ -2709,16 +2709,107 @@
 d.fab);%0A
+        // TODO: Add animations (elevation change and inc when pressed), + haptic response%0A
         
@@ -3191,46 +3191,8 @@
 ing%0A
-                    mCurrentTime = 0;%0A
     
@@ -3496,51 +3496,8 @@
 le);
-%0A%0A                    // Save the recording
 %0A   
@@ -3562,32 +3562,1014 @@
 g;%0A            %7D
+%0A%0A        %7D);%0A%0A        fab.setOnLongClickListener(new View.OnLongClickListener() %7B%0A            public boolean onLongClick(View view) %7B%0A                if(mCurrentTime %3E 0) // No action if we didn't start the recording%0A                %7B%0A                    // Resore state%0A                    fab.setImageResource(android.R.drawable.ic_media_play);%0A                    mIsRecording = false;%0A                    mCurrentTime = 0;%0A%0A                    // Change GUI%0A                    // TODO: Hide gui elem%0A                    mRecoringText.setText(%22Inactive%22);%0A%0A                    // Stop the recording%0A                    mRecordingHandler.removeCallbacks(mRecordingRunable);%0A%0A                    // Save the recording%0A                    Snackbar.make(view, %22Saving data...%22, Snackbar.LENGTH_LONG)%0A                            .setAction(%22Action%22, null)%0A                            .show();%0A                %7D%0A%0A                return true; // No other listener called%0A            %7D
 %0A        %7D);%0A%0A  

Reset timer when long click on the fab