package com.example.patrice.firebase_database;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


public class main02 extends AppCompatActivity{
    private Context mContext;
    private Activity mActivity;

    private LinearLayout Layout;
    private TextView mTextView;
    private Button mButtonRed;
    private Button mButtonGreen;
    private Button mButtonBlue;

    // Get the reference of firebase real time database
    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    // Get the reference of firebase real time database child layout_color
    DatabaseReference mLayoutColorRef = mRootRef.child("layout_color");

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

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

        // Get the widget reference from XML layout
        Layout = (LinearLayout) findViewById(R.id.Layout);
        mTextView = (TextView) findViewById(R.id.tv);
        mButtonRed = (Button) findViewById(R.id.btn_red);
        mButtonGreen = (Button) findViewById(R.id.btn_green);
        mButtonBlue = (Button) findViewById(R.id.btn_blue);

        //Layout.setBackgroundColor(Color.parseColor("#01ff90"));

        //Layout.setBackgroundColor(Color.RED);

    }

    @Override
    protected void onStart()
    {
        super.onStart();

        // Catch the firebase database change on real time
        mLayoutColorRef.addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                // Do something when data changed on firebase real time database
                String colorString = dataSnapshot.getValue(String.class);

                // Change the layout background color
                Layout.setBackgroundColor( Color.parseColor(colorString) );

                // Also display the changed value on text view
                mTextView.setText(colorString);

                Toast.makeText(getApplicationContext(), "Value received : " + colorString , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError)
            {

            }
        });

        // Update the fire base database value on real time
        mButtonRed.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonRed.getText());
                Toast.makeText(mContext,"Value changed : " + mButtonRed.getText(),Toast.LENGTH_LONG).show();
            }
        });

        mButtonGreen.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonGreen.getText());
                Toast.makeText(mContext,"Value changed : " + mButtonGreen.getText(),Toast.LENGTH_LONG).show();
            }
        });

        mButtonBlue.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonBlue.getText());
                Toast.makeText(mContext,"Value changed : "   + mButtonBlue.getText(),Toast.LENGTH_LONG).show();
            }
        });
    }
}