package com.hannesdorfmann.mosby.sample.mail.statistics;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpViewStateDelegateImpl;
import com.hannesdorfmann.mosby.mvp.delegate.MvpViewStateDelegateCallback;
import com.hannesdorfmann.mosby.mvp.viewstate.ViewState;
import com.hannesdorfmann.mosby.sample.mail.MailApplication;
import com.hannesdorfmann.mosby.sample.mail.R;
import com.hannesdorfmann.mosby.sample.mail.base.view.viewstate.AuthParcelableDataViewState;
import com.hannesdorfmann.mosby.sample.mail.model.mail.statistics.MailStatistics;

/**
 * @author Hannes Dorfmann
 */
public class StatisticsDialog extends DialogFragment
    implements StatisticsView, MvpViewStateDelegateCallback<StatisticsView, StatisticsPresenter> {

  @InjectView(R.id.contentView) RecyclerView contentView;
  @InjectView(R.id.loadingView) View loadingView;
  @InjectView(R.id.errorView) TextView errorView;
  @InjectView(R.id.authView) View authView;

  StatisticsPresenter presenter;
  ViewState<StatisticsView> viewState;
  MailStatistics data;
  StatisticsAdapter adapter;

  private FragmentMvpDelegate<StatisticsView, StatisticsPresenter> delegate =
      new FragmentMvpViewStateDelegateImpl<>(this);

  //
  // DELEGATE callback
  //

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    delegate.onCreate(savedInstanceState);
  }

  @Override public void onDestroy() {
    super.onDestroy();
    delegate.onDestroy();
  }

  @Override public void onPause() {
    super.onPause();
    delegate.onPause();
  }

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

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

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

    getDialog().setTitle(R.string.menu_statistics);

    ButterKnife.inject(this, view);
    adapter = new StatisticsAdapter(getActivity());
    contentView.setAdapter(adapter);
    contentView.setLayoutManager(new LinearLayoutManager(getActivity()));
  }

  @Override public void onStart() {
    super.onStart();
    delegate.onStart();
  }

  @Override public void onStop() {
    super.onStop();
    delegate.onStop();
  }

  @Override public void onAttach(Activity activity) {
    super.onAttach(activity);
    delegate.onAttach(activity);
  }

  @Override public void onDetach() {
    super.onDetach();
    delegate.onDetach();
  }

  @Override public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    delegate.onActivityCreated(savedInstanceState);
  }

  @Override public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    delegate.onSaveInstanceState(outState);
  }

  //
  // End delegates
  //

  @Override public void showAuthenticationRequired() {
    contentView.setVisibility(View.GONE);
    errorView.setVisibility(View.GONE);
    loadingView.setVisibility(View.GONE);
    authView.setVisibility(View.VISIBLE);

    ((AuthParcelableDataViewState<MailStatistics, StatisticsView>) viewState).setShowingAuthenticationRequired();
  }

  @Override public void showLoading(boolean pullToRefresh) {
    contentView.setVisibility(View.GONE);
    errorView.setVisibility(View.GONE);
    loadingView.setVisibility(View.VISIBLE);
    authView.setVisibility(View.GONE);

    ((AuthParcelableDataViewState<MailStatistics, StatisticsView>) viewState).setStateShowLoading(
        pullToRefresh);
  }

  @Override public void showContent() {
    contentView.setVisibility(View.VISIBLE);
    errorView.setVisibility(View.GONE);
    loadingView.setVisibility(View.GONE);
    authView.setVisibility(View.GONE);

    ((AuthParcelableDataViewState<MailStatistics, StatisticsView>) viewState).setStateShowContent(
        data);
  }

  @Override public void showError(Throwable e, boolean pullToRefresh) {
    contentView.setVisibility(View.GONE);
    errorView.setVisibility(View.VISIBLE);
    loadingView.setVisibility(View.GONE);
    authView.setVisibility(View.GONE);
    ((AuthParcelableDataViewState<MailStatistics, StatisticsView>) viewState).setStateShowError(e,
        pullToRefresh);
  }

  @Override public void setData(MailStatistics data) {
    this.data = data;
    adapter.setItems(data.getMailsCounts());
    adapter.notifyDataSetChanged();
  }

  @Override public void loadData(boolean pullToRefresh) {
    presenter.loadStatistics();
  }

  @Override public ViewState<StatisticsView> getViewState() {
    return viewState;
  }

  @Override public void setViewState(ViewState<StatisticsView> viewState) {
    this.viewState = viewState;
  }

  @Override public ViewState<StatisticsView> createViewState() {
    return new AuthParcelableDataViewState<MailStatistics, StatisticsView>();
  }

  @Override public void setRestoringViewState(boolean restoringViewState) {
    // Not needed
  }

  @Override public boolean isRestoringViewState() {
    // Not needed
    return false;
  }

  @Override public void onViewStateInstanceRestored(boolean instanceStateRetained) {
    // Not needed
  }

  @Override public void onNewViewStateInstance() {
    loadData(false);
  }

  @Override public StatisticsPresenter createPresenter() {
    return DaggerStatisticsComponent.builder()
        .mailAppComponent(MailApplication.getMailComponents())
        .build()
        .presenter();
  }

  @Override public StatisticsPresenter getPresenter() {
    return presenter;
  }

  @Override public void setPresenter(StatisticsPresenter presenter) {
    this.presenter = presenter;
  }

  @Override public StatisticsView getMvpView() {
    return this;
  }

  @Override public boolean isRetainingInstance() {
    return getRetainInstance();
  }
}

@@ -80,16 +80,43 @@
 tivity;%0A
+import android.app.Dialog;%0A
 import a
@@ -129,24 +129,67 @@
 .os.Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import andro
@@ -266,16 +266,63 @@
 agment;%0A
+import android.support.v7.app.AppCompatDialog;%0A
 import a
@@ -2669,61 +2669,8 @@
 );%0A%0A
-    getDialog().setTitle(R.string.menu_statistics);%0A%0A
     
@@ -3605,24 +3605,253 @@
 gates%0A  //%0A%0A
+  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) %7B%0A%0A    AppCompatDialog dialog = new AppCompatDialog(getActivity(),  getTheme());%0A    dialog.setTitle(R.string.menu_statistics);%0A    return dialog;%0A  %7D%0A%0A
   @Override 

Use AppCompatDialog