/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pyamsoft.powermanager.app.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.pyamsoft.powermanager.R;
import com.pyamsoft.powermanager.app.manager.ManagerFragment;

public class MainFragment extends Fragment {

  @Nullable @BindView(R.id.toggle_wifi_test) Button toggleWifi;
  @Nullable @BindView(R.id.toggle_data_test) Button toggleData;
  @Nullable @BindView(R.id.toggle_bluetooth_test) Button toggleBluetooth;
  @Nullable @BindView(R.id.toggle_sync_test) Button toggleSync;
  @Nullable @BindView(R.id.toggle_all_off) Button toggleOff;
  @Nullable @BindView(R.id.toggle_all_on) Button toggleOn;

  @Nullable private Unbinder unbinder;

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_main, container, false);
    unbinder = ButterKnife.bind(this, view);
    return view;
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    assert unbinder != null;
    unbinder.unbind();
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupButtons();
  }

  private void setupButtons() {
    assert toggleWifi != null;
    toggleWifi.setOnClickListener(view -> {
      getFragmentManager().beginTransaction()
          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_WIFI))
          .addToBackStack(null)
          .commit();
      final FragmentActivity fragmentActivity = getActivity();
      if (fragmentActivity instanceof MainActivity) {
        final MainActivity mainActivity = (MainActivity) fragmentActivity;
        mainActivity.setActionBarUpEnabled(true);
      }
    });

    assert toggleData != null;
    toggleData.setOnClickListener(view -> {
      getFragmentManager().beginTransaction()
          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_DATA))
          .addToBackStack(null)
          .commit();
      final FragmentActivity fragmentActivity = getActivity();
      if (fragmentActivity instanceof MainActivity) {
        final MainActivity mainActivity = (MainActivity) fragmentActivity;
        mainActivity.setActionBarUpEnabled(true);
      }
    });

    assert toggleBluetooth != null;
    toggleBluetooth.setOnClickListener(view -> {
      getFragmentManager().beginTransaction()
          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_BLUETOOTH))
          .addToBackStack(null)
          .commit();
      final FragmentActivity fragmentActivity = getActivity();
      if (fragmentActivity instanceof MainActivity) {
        final MainActivity mainActivity = (MainActivity) fragmentActivity;
        mainActivity.setActionBarUpEnabled(true);
      }
    });

    assert toggleSync != null;
    toggleSync.setOnClickListener(view -> {
      getFragmentManager().beginTransaction()
          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_SYNC))
          .addToBackStack(null)
          .commit();
      final FragmentActivity fragmentActivity = getActivity();
      if (fragmentActivity instanceof MainActivity) {
        final MainActivity mainActivity = (MainActivity) fragmentActivity;
        mainActivity.setActionBarUpEnabled(true);
      }
    });
  }
}

@@ -1163,395 +1163,8 @@
  %7B%0A%0A
-  @Nullable @BindView(R.id.toggle_wifi_test) Button toggleWifi;%0A  @Nullable @BindView(R.id.toggle_data_test) Button toggleData;%0A  @Nullable @BindView(R.id.toggle_bluetooth_test) Button toggleBluetooth;%0A  @Nullable @BindView(R.id.toggle_sync_test) Button toggleSync;%0A  @Nullable @BindView(R.id.toggle_all_off) Button toggleOff;%0A  @Nullable @BindView(R.id.toggle_all_on) Button toggleOn;%0A%0A
   @N
@@ -1769,2195 +1769,9 @@
 ;%0A  
-  setupButtons();%0A  %7D%0A%0A  private void setupButtons() %7B%0A    assert toggleWifi != null;%0A    toggleWifi.setOnClickListener(view -%3E %7B%0A      getFragmentManager().beginTransaction()%0A          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_WIFI))%0A          .addToBackStack(null)%0A          .commit();%0A      final FragmentActivity fragmentActivity = getActivity();%0A      if (fragmentActivity instanceof MainActivity) %7B%0A        final MainActivity mainActivity = (MainActivity) fragmentActivity;%0A        mainActivity.setActionBarUpEnabled(true);%0A      %7D%0A    %7D);%0A%0A    assert toggleData != null;%0A    toggleData.setOnClickListener(view -%3E %7B%0A      getFragmentManager().beginTransaction()%0A          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_DATA))%0A          .addToBackStack(null)%0A          .commit();%0A      final FragmentActivity fragmentActivity = getActivity();%0A      if (fragmentActivity instanceof MainActivity) %7B%0A        final MainActivity mainActivity = (MainActivity) fragmentActivity;%0A        mainActivity.setActionBarUpEnabled(true);%0A      %7D%0A    %7D);%0A%0A    assert toggleBluetooth != null;%0A    toggleBluetooth.setOnClickListener(view -%3E %7B%0A      getFragmentManager().beginTransaction()%0A          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_BLUETOOTH))%0A          .addToBackStack(null)%0A          .commit();%0A      final FragmentActivity fragmentActivity = getActivity();%0A      if (fragmentActivity instanceof MainActivity) %7B%0A        final MainActivity mainActivity = (MainActivity) fragmentActivity;%0A        mainActivity.setActionBarUpEnabled(true);%0A      %7D%0A    %7D);%0A%0A    assert toggleSync != null;%0A    toggleSync.setOnClickListener(view -%3E %7B%0A      getFragmentManager().beginTransaction()%0A          .replace(R.id.main_container, ManagerFragment.newInstance(ManagerFragment.TYPE_SYNC))%0A          .addToBackStack(null)%0A          .commit();%0A      final FragmentActivity fragmentActivity = getActivity();%0A      if (fragmentActivity instanceof MainActivity) %7B%0A        final MainActivity mainActivity = (MainActivity) fragmentActivity;%0A        mainActivity.setActionBarUpEnabled(true);%0A      %7D%0A    %7D);%0A  
 %7D
+%0A
 %0A%7D%0A

Remove test buttons