package co.arcs.groove.basking;

import android.app.Fragment;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.common.eventbus.Subscribe;

import co.arcs.groove.basking.BaskingSyncService.SyncBinder;
import co.arcs.groove.basking.event.impl.SyncEvent;

public class MainFragment extends Fragment {

    private Button syncButton;
    private BaskingSyncService.SyncBinder serviceBinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(getActivity(), BaskingSyncService.class);

        boolean bound = getActivity().bindService(i, serviceConnection, Service.BIND_AUTO_CREATE);
        if (!bound) {
            throw new RuntimeException("Failed to bind to sync service, cannot continue");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        syncButton = (Button) view.findViewById(R.id.sync_button);
        syncButton.setOnClickListener(syncButtonOnClickListener);
    }

    @Override
    public void onResume() {
        super.onResume();

        boolean syncOngoing = (serviceBinder != null) && (serviceBinder.isSyncOngoing());
        boolean hasLoginCredentials = App.getAppPreferences().hasLoginCredentials();
        syncButton.setEnabled(!syncOngoing && hasLoginCredentials);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (serviceBinder != null) {
            serviceBinder.getSyncEventBus().unregister(this);
        }
        getActivity().unbindService(serviceConnection);
    }

    private final ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            MainFragment.this.serviceBinder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MainFragment.this.serviceBinder = (SyncBinder) service;
            serviceBinder.getSyncEventBus().register(MainFragment.this);
        }
    };

    private OnClickListener syncButtonOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            syncButton.setEnabled(false);
            getActivity().startService(BaskingSyncService.newStartIntent(getActivity()));
        }
    };

    @Subscribe
    public void onEvent(SyncEvent.Finished e) {
        syncButton.setEnabled(true);
    }

    @Subscribe
    public void onEvent(SyncEvent.FinishedWithError e) {
        syncButton.setEnabled(true);
    }
}

@@ -194,16 +194,133 @@
 ection;%0A
+import android.content.SharedPreferences;%0Aimport android.content.SharedPreferences.OnSharedPreferenceChangeListener;%0A
 import a
@@ -1843,22 +1843,21 @@
  void on
-Resume
+Start
 () %7B%0A   
@@ -1873,193 +1873,336 @@
 r.on
-Resume();%0A%0A        boolean syncOngoing = (serviceBinder != null) && (serviceBinder.isSyncOngoing());%0A        boolean hasLoginCredentials = App.getAppPreferences().hasLoginCredential
+Start();%0A        App.getAppPreferences()%0A                .getPrefs()%0A                .registerOnSharedPreferenceChangeListener(preferenceChangeListener);%0A%0A        syncButton.setEnabled(canSync());%0A    %7D%0A%0A    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A        App.getAppPreferences()%0A                .getPref
 s()
-;
 %0A   
@@ -2210,65 +2210,84 @@
     
-syncButton.setEnabled(!syncOngoing && hasLoginCredentials
+        .unregisterOnSharedPreferenceChangeListener(preferenceChangeListener
 );%0A 
@@ -2531,24 +2531,322 @@
 on);%0A    %7D%0A%0A
+    private final OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener() %7B%0A        @Override%0A        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) %7B%0A            syncButton.setEnabled(canSync());%0A        %7D%0A    %7D;%0A%0A
     private 
@@ -3824,11 +3824,269 @@
 );%0A    %7D
+%0A%0A    private boolean canSync() %7B%0A        boolean syncOngoing = (serviceBinder != null) && (serviceBinder.isSyncOngoing());%0A        boolean haveCredentials = App.getAppPreferences().hasLoginCredentials();%0A        return !syncOngoing && haveCredentials;%0A    %7D
 %0A%7D%0A

Fix sync button status not updating when login details added.