package io.uscool.fuelfriend.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.arlib.floatingsearchview.FloatingSearchView;

import io.uscool.fuelfriend.R;
import io.uscool.fuelfriend.fragment.BaseSearchFragment;
import io.uscool.fuelfriend.fragment.SearchViewFragment;
import io.uscool.fuelfriend.helper.PreferenceHelper;
import io.uscool.fuelfriend.service.AlarmReceiver;
import io.uscool.fuelfriend.service.DownloadResultReceiver;
import io.uscool.fuelfriend.service.DownloadService;


public class MainActivity extends AppCompatActivity
        implements BaseSearchFragment.BaseSearchFragmentCallbacks, NavigationView.OnNavigationItemSelectedListener {


//    private final String TAG = "MainActivity";

    private DrawerLayout mDrawerLayout;
    private DownloadResultReceiver mReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        startDownloadService();
        setupServiceReceiver();
        onStartAlarm();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        showFragment(new SearchViewFragment());

    }
  /*  private void startDownloadService() {
        mReceiver = new DownloadResultReceiver(new Handler());
        mReceiver.setReceiver(this);

        Intent intent = new Intent(Intent.ACTION_SYNC, null, this, DownloadService.class);
        intent.putExtra("receiver", mReceiver);
        intent.putExtra("requestId", 101);

        startService(intent);
    }
*/
    public void setupServiceReceiver() {
        mReceiver = new DownloadResultReceiver(new Handler());
        Intent intent = new Intent(getApplicationContext(), DownloadService.class);
        intent.putExtra("receiver", mReceiver);
        if(!PreferenceHelper.isSynced(getApplicationContext())) {
            startService(intent);
        }
        // This is where we specify what happens when data is received from the
        // service
        mReceiver.setReceiver(new DownloadResultReceiver.Receiver() {
            @Override
            public void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode == RESULT_OK) {
                    String resultValue = resultData.getString("resultValue");
                    Toast.makeText(MainActivity.this, resultValue, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void onStartAlarm() {
        // Construct an intent that will execute the AlarmReceiver
        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        mReceiver = new DownloadResultReceiver(new Handler());
//        mReceiver.setReceiver(this);
        intent.putExtra("receiver", mReceiver);
        // Create a PendingIntent to be triggered when the alarm goes off
        PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // Setup periodic alarm every 5 seconds
        long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
        int intervalMillis = 5000*12*60; // 1 hour
        AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, alarmPendingIntent);
    }


    @Override
    public void onAttachSearchViewToDrawer(FloatingSearchView searchView) {
        searchView.attachNavigationDrawerToMenuButton(mDrawerLayout);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }



    private void showFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment).commit();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            Intent dbmanager = new Intent(getApplicationContext(),AndroidDatabaseManagerActivity.class);
            startActivity(dbmanager);
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {
        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

   /* @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        switch (resultCode) {
            case DownloadService.STATUS_RUNNING:

                setProgressBarIndeterminateVisibility(true);
                break;
            case DownloadService.STATUS_FINISHED:
                *//* Hide progress & extract result from bundle *//*
                setProgressBarIndeterminateVisibility(false);
//                Toast.makeText(this, "Service Completed Successfully",
//                        Toast.LENGTH_SHORT).show();

                break;
            case DownloadService.STATUS_ERROR:
                *//* Handle the error *//*
                String error = resultData.getString(Intent.EXTRA_TEXT);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                break;
        }
    }*/
}