/*
 * 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.pasterino.app.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.pyamsoft.pasterino.BuildConfig;
import com.pyamsoft.pasterino.R;
import com.pyamsoft.pasterino.app.service.PasteService;
import com.pyamsoft.pydroid.base.activity.DonationActivityBase;
import com.pyamsoft.pydroid.support.AdvertisementView;
import com.pyamsoft.pydroid.support.RatingDialog;
import com.pyamsoft.pydroid.util.StringUtil;

public final class MainActivity extends DonationActivityBase
    implements RatingDialog.ChangeLogProvider {

  @BindView(R.id.ad_view) AdvertisementView adView;
  @BindView(R.id.main_toolbar) Toolbar toolbar;
  private Unbinder unbinder;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    setTheme(R.style.Theme_Pasterino_Light);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    unbinder = ButterKnife.bind(this);
    adView.create();
    setupAppBar();
  }

  @Override protected void onPostResume() {
    super.onPostResume();
    // Do not show rating dialog for first version
    //RatingDialog.showRatingDialog(this, this);

    if (PasteService.isRunning()) {
      showMainFragment();
    } else {
      showAccessibilityRequestFragment();
    }
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    adView.destroy();
    unbinder.unbind();
  }

  private void setupAppBar() {
    toolbar.setTitle(getString(R.string.app_name));
    setSupportActionBar(toolbar);
    setActionBarUpEnabled(false);
  }

  private void showAccessibilityRequestFragment() {
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_container, new AccessibilityRequestFragment())
        .commit();
  }

  private void showMainFragment() {
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_container, new MainSettingsFragment())
        .commit();
  }

  @NonNull @Override protected String getPlayStoreAppPackage() {
    return getPackageName();
  }

  @Override public void showAd() {
    adView.show(false);
  }

  @Override public void hideAd() {
    adView.hide();
  }

  @NonNull @Override public Spannable getChangeLogText() {
    // The changelog text
    final String title = "What's New in Version " + BuildConfig.VERSION_NAME;
    final String line1 = "FEATURE: This change log screen";

    // Turn it into a spannable
    final Spannable spannable = StringUtil.createLineBreakBuilder(title, line1);

    int start = 0;
    int end = title.length();
    final int largeSize =
        StringUtil.getTextSizeFromAppearance(this, android.R.attr.textAppearanceLarge);
    final int largeColor =
        StringUtil.getTextColorFromAppearance(this, android.R.attr.textAppearanceLarge);
    final int smallSize =
        StringUtil.getTextSizeFromAppearance(this, android.R.attr.textAppearanceSmall);
    final int smallColor =
        StringUtil.getTextColorFromAppearance(this, android.R.attr.textAppearanceSmall);

    StringUtil.boldSpan(spannable, start, end);
    StringUtil.sizeSpan(spannable, start, end, largeSize);
    StringUtil.colorSpan(spannable, start, end, largeColor);

    start += end + 2;
    end += 2 + line1.length();

    StringUtil.sizeSpan(spannable, start, end, smallSize);
    StringUtil.colorSpan(spannable, start, end, smallColor);

    return spannable;
  }

  @Override public int getChangeLogIcon() {
    return R.mipmap.ic_launcher;
  }

  @NonNull @Override public String getChangeLogPackageName() {
    return getPackageName();
  }

  @Override public int getChangeLogVersion() {
    return BuildConfig.VERSION_CODE;
  }
}

@@ -1116,63 +1116,8 @@
 se;%0A
-import com.pyamsoft.pydroid.support.AdvertisementView;%0A
 impo
@@ -1322,60 +1322,8 @@
  %7B%0A%0A
-  @BindView(R.id.ad_view) AdvertisementView adView;%0A
   @B
@@ -1559,52 +1559,8 @@
 te);
-%0A    setContentView(R.layout.activity_main);
 %0A%0A  
@@ -1600,29 +1600,8 @@
 s);%0A
-    adView.create();%0A
     
@@ -1646,113 +1646,183 @@
 ted 
-void onPostResume() %7B%0A    super.onPostResume();%0A    // Do not show rating dialog for first version
+int bindActivityToView() %7B%0A    setContentView(R.layout.activity_main);%0A    return R.id.ad_view;%0A  %7D%0A%0A  @Override protected void onPostResume() %7B%0A    super.onPostResume();
 %0A    
-//
 Rati
@@ -2057,30 +2057,8 @@
 ();%0A
-    adView.destroy();%0A
     
@@ -2718,131 +2718,8 @@
  %7D%0A%0A
-  @Override public void showAd() %7B%0A    adView.show(false);%0A  %7D%0A%0A  @Override public void hideAd() %7B%0A    adView.hide();%0A  %7D%0A%0A
   @N

Refactor into base