content: /*
   * Copyright (C) 2015 The Android Open Source Project
   *
   * 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 <%= appPackage %>.taskdetail;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.TextView;
  
  import <%= appPackage %>.R;
  import <%= appPackage %>.addedittask.AddEditTaskActivity;
  import <%= appPackage %>.addedittask.AddEditTaskFragment;
  import <%= appPackage %>.di.ActivityScoped;
  import com.google.common.base.Preconditions;
  
  import javax.inject.Inject;
  
  import dagger.android.support.DaggerFragment;
  
  /**
   * Main UI for the task detail screen.
   */
  @ActivityScoped
  public class TaskDetailFragment extends DaggerFragment implements TaskDetailContract.View {
  
      @NonNull
      private static final String ARGUMENT_TASK_ID = "TASK_ID";
  
      @NonNull
      private static final int REQUEST_EDIT_TASK = 1;
      @Inject
      String taskId;
      @Inject
      TaskDetailContract.Presenter mPresenter;
      private TextView mDetailTitle;
      private TextView mDetailDescription;
      private CheckBox mDetailCompleteStatus;
  
      @Inject
      public TaskDetailFragment() {
      }
  
  
      @Override
      public void onResume() {
          super.onResume();
          mPresenter.takeView(this);
      }
  
      @Override
      public void onDestroy() {
          mPresenter.dropView();
          super.onDestroy();
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View root = inflater.inflate(R.layout.taskdetail_frag, container, false);
          setHasOptionsMenu(true);
          mDetailTitle = root.findViewById(R.id.task_detail_title);
          mDetailDescription = root.findViewById(R.id.task_detail_description);
          mDetailCompleteStatus = root.findViewById(R.id.task_detail_complete);
  
          // Set up floating action button
          FloatingActionButton fab = getActivity().findViewById(R.id.fab_edit_task);
  
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  mPresenter.editTask();
              }
          });
  
          return root;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.menu_delete:
                  mPresenter.deleteTask();
                  return true;
          }
          return false;
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.taskdetail_fragment_menu, menu);
      }
  
      @Override
      public void setLoadingIndicator(boolean active) {
          if (active) {
              mDetailTitle.setText("");
              mDetailDescription.setText(getString(R.string.loading));
          }
      }
  
      @Override
      public void hideDescription() {
          mDetailDescription.setVisibility(View.GONE);
      }
  
      @Override
      public void hideTitle() {
          mDetailTitle.setVisibility(View.GONE);
      }
  
      @Override
      public void showDescription(@NonNull String description) {
          mDetailDescription.setVisibility(View.VISIBLE);
          mDetailDescription.setText(description);
      }
  
      @Override
      public void showCompletionStatus(final boolean complete) {
          Preconditions.checkNotNull(mDetailCompleteStatus);
  
          mDetailCompleteStatus.setChecked(complete);
          mDetailCompleteStatus.setOnCheckedChangeListener(
                  new CompoundButton.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                          if (isChecked) {
                              mPresenter.completeTask();
                          } else {
                              mPresenter.activateTask();
                          }
                      }
                  });
      }
  
      @Override
      public void showEditTask(@NonNull String taskId) {
          Intent intent = new Intent(getContext(), AddEditTaskActivity.class);
          intent.putExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID, taskId);
          startActivityForResult(intent, REQUEST_EDIT_TASK);
      }
  
      @Override
      public void showTaskDeleted() {
          getActivity().finish();
      }
  
      public void showTaskMarkedComplete() {
          Snackbar.make(getView(), getString(R.string.task_marked_complete), Snackbar.LENGTH_LONG)
                  .show();
      }
  
      @Override
      public void showTaskMarkedActive() {
          Snackbar.make(getView(), getString(R.string.task_marked_active), Snackbar.LENGTH_LONG)
                  .show();
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == REQUEST_EDIT_TASK) {
              // If the task was edited successfully, go back to the list.
              if (resultCode == Activity.RESULT_OK) {
                  getActivity().finish();
              }
          }
      }
  
      @Override
      public void showTitle(@NonNull String title) {
          mDetailTitle.setVisibility(View.VISIBLE);
          mDetailTitle.setText(title);
      }
  
      @Override
      public void showMissingTask() {
          mDetailTitle.setText("");
          mDetailDescription.setText(getString(R.string.no_data));
      }
  
      @Override
      public boolean isActive() {
          return isAdded();
      }
  
  }
  