package com.example.fitbot.ui.activities;

import static com.example.fitbot.util.Networking.sendIpAddress;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.example.fitbot.R;
import com.example.fitbot.pepper.Pepper;
import com.example.fitbot.util.NavigationManager;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    // Variables
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;
    Button startButton;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Set full screen mode to hide status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        startButton = findViewById(R.id.startButtonMain);
        startButton.setOnClickListener(v -> {
            Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bicepvideo);
            Intent intent = new Intent(MainActivity.this, FitnessActivity.class);
            intent.putExtra("videoUri", videoUri);
            startActivity(intent);
        });

        // Set up the UI
        setUpUi();

        // Hide system UI
        NavigationManager.hideSystemUI(this);
        sendIpAddress(this);
    }

    private void setUpUi() {
        /*---Hooks---*/
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);
        startButton = findViewById(R.id.startButtonMain);

        // Hide the action bar
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        NavigationManager.setupButtonNavigation(this, R.id.startButtonMain, FitnessActivity.class);
        NavigationManager.setupButtonNavigation(this, R.id.helpButtonMain, HelpActivity.class);

        /*---Tool Bar---*/
        setSupportActionBar(toolbar); // Make the toolbar act as the action bar
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false); // Remove the title from the toolbar
        }

        /*---Navigation Drawer Menu---*/
        navigationView.bringToFront(); // Make the navigation drawer menu clickable

        ActionBarDrawerToggle toggle = new // Create a toggle for the navigation drawer
                ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                    }

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                    }
                };
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState(); // Synchronize the state of the navigation drawer
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            NavigationManager.hideSystemUI(this);
        }
    }

    @Override
    public void onBackPressed() { // Close the navigation drawer when the back button is pressed
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


}


