package com.example.floatingactionbutton01;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.Random;


public class MainActivity extends Activity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private FloatingActionButton mFAB;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.cLayout);
        mFAB = (FloatingActionButton) findViewById(R.id.fab);

        // Set a click listener for Floating Action Button
        mFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Generate a random color
                int color = getRandomHSVColor();

                // Change the layout background color
                mCLayout.setBackgroundColor(color);

                // Initialize a new Snackbar instance
                Snackbar snackbar = Snackbar.make(
                        mCLayout,
                        "Layout color changed.",
                        Snackbar.LENGTH_LONG);

                // Set an action for Snackbar
                // Set a click listener for Snackbar action button
                snackbar.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Do something when Snackbar action button clicked
                    }
                });

                // Display the Snackbar
                snackbar.show();
            }
        });
    }

    // Custom method to generate random HSV color
    public static int getRandomHSVColor(){
        Random rand = new Random();
        // Generate a random hue value between 0 to 360
        int hue = rand.nextInt(361);
        // We make the color depth full
        float saturation = 1.0f;
        // We make a full bright color
        float value = 1.0f;
        // We avoid color transparency
        int alpha = 255;
        // Finally, generate the color
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        // Return the color
        return color;
    }
}