package com.pot.timerly;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * Created by pot on 04/02/16.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private static boolean colorInitialized = false;
    private static int backgroundColor; // Default color
    private static int selectedColor; // When item selected

    private List<RecordingItem> mDataset;

    private Context mContext;

    private final String FILE_SAVE_RECORDING = "recording.csv";

    // Class which correspond to each element of our RecyclerView
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        // Each data item is just a string in this case

        // Should contain:
        // - Date of recording
        // - Duration

        public TextView mDurationText;
        public TextView mDateText;
        public Button mDeleteButton;

        public ViewHolder(View v) {
            super(v);
            v.setOnClickListener(this);

            mDurationText = (TextView) v.findViewById(R.id.duration_text);
            mDateText = (TextView) v.findViewById(R.id.date_text);
            mDeleteButton = (Button) v.findViewById(R.id.delete_button);
            mDeleteButton.setOnClickListener(this);

            // TODO: mDeleteButton.setOnClickListener(); << PB with the click of the view holder ?!

            if(!colorInitialized) {
                // TODO: Use theme color instead of a cst color
                backgroundColor = ContextCompat.getColor(v.getContext(), R.color.background_material_light);
                selectedColor = ContextCompat.getColor(v.getContext(), R.color.item_selected);
                colorInitialized = true;
            }
        }

        @Override
        public void onClick(final View view) {
            if (view.getId() == mDeleteButton.getId()) { // Button clicked

                // Allow the restoration of the object
                final RecordingItem savedItem = new RecordingItem(mDataset.get(getAdapterPosition())); // Copy the item before removing it
                final int savedPosition = getAdapterPosition();

                Snackbar.make(view, "Recording deleted", Snackbar.LENGTH_LONG)
                        .setAction("Undo", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                addItem(savedPosition, savedItem);
                            }
                        })
                        .show();

                // Delete the object from the database
                removeItem(getAdapterPosition());
            } else { // Item clicked
                // TODO: Dirty !! (should use a member which store the state)
                boolean isSelected = ((ColorDrawable)view.getBackground()) != null && ((ColorDrawable)view.getBackground()).getColor() == selectedColor;

                int finalRadius = (int)Math.hypot(view.getWidth()/2, view.getHeight()/2);

                if (isSelected) {
                    // Reverse animation
                    // TODO: clipping issue when animation finished
                    view.setBackgroundColor(backgroundColor);
                    Animator anim = ViewAnimationUtils.createCircularReveal(view,
                            (int) view.getWidth() / 2,
                            (int) view.getHeight() / 2,
                            finalRadius,
                            0);
                    view.setBackgroundColor(selectedColor);

                    // Restore the original background when the animation is done
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            view.setBackgroundColor(backgroundColor);
                        }
                    });
                    anim.start();
                } else {
                    // Revelation
                    Animator anim = ViewAnimationUtils.createCircularReveal(view,
                            (int) view.getWidth() / 2,
                            (int) view.getHeight() / 2,
                            0,
                            finalRadius);
                    view.setBackgroundColor(selectedColor);
                    anim.start();
                }
            }
        }
    }

    public void addItem(int position, RecordingItem newItem) {
        mDataset.add(position, newItem);
        notifyItemInserted(position);
        saveDatabase();
    }

    public void addItem(RecordingItem newItem) {
        mDataset.add(newItem);
        notifyItemInserted(mDataset.size());
        saveDatabase();
    }

    public void removeItem(int position) {
        mDataset.remove(position);
        notifyItemRemoved(position);
        saveDatabase();
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(Context context) {
        this.mContext = context;

        final List<RecordingItem> myDataset = new ArrayList<>();

        try {
            FileInputStream file = mContext.openFileInput(FILE_SAVE_RECORDING);

            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLong()) {
                long duration = scanner.nextLong();
                Date date = scanner.hasNextLong() ? new Date(scanner.nextLong()) : new Date();

                myDataset.add(new RecordingItem(duration, date));
            }

            String theString = scanner.hasNext() ? scanner.next() : "";



            file.close();
        } catch (FileNotFoundException e) {
            Log.d("Timerly", "File don't: first launch");

        } catch (IOException e) {
            e.printStackTrace();
            // FILE NOT CLOSED !!!
        }

        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.duration_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element

        long duration = mDataset.get(position).getDuration();
        long minutes = duration/(60*1000);
        duration -= minutes*(60*1000);
        long seconds = duration/(1000);
        duration -= seconds*1000;
        long milisecond = duration/100; // Only keep the decimal
        holder.mDurationText.setText(String.format("Duration: %dmin %02d.%dsec", minutes, seconds, milisecond));

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy - HH:mm"); // TODO: Replace that by LocalDateTime on java 8
        holder.mDateText.setText(dateFormat.format(mDataset.get(position).getDate()));

        // TODO: Restore also the state (selected or not) >> change background accordingly

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public void saveDatabase() {

        FileOutputStream outputStream;

        try {
            outputStream = mContext.openFileOutput(FILE_SAVE_RECORDING, Context.MODE_PRIVATE);

            for (RecordingItem item : mDataset) {
                outputStream.write(item.toString().getBytes());
                outputStream.write('\n');
            }

            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            //Snackbar() // TODO
        }
    }
}

@@ -7940,18 +7940,18 @@
 mat(%22dd/
-mm
+MM
 /yyyy - 

Correct a format error