package org.sil.bloom.reader;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import org.sil.bloom.reader.models.BookCollection;
import org.sil.bloom.reader.models.ExtStorageUnavailableException;

 * Created by Thomson on 10/20/2017.
 * Showing a bookshelf is much like our main activity, which also shows a list of books (and shelves--
 * something that just might come to this activity one day). For now we are using the same layout,
 * with the action bar changed to show the shelf icon, title and background and a back button instead
 * of the menu.
 * There are just a few things we need to do differently.
 * It may at some point be worth refactoring to extract a common base class, but for now, I prefer
 * to see all the logic in the class that primarily uses it and for which it was originally written,
 * MainActivity itself. This will also tend to facilitate git comparisons.
public class ShelfActivity extends MainActivity {

    private String filter;
    protected void onCreate(Bundle savedInstanceState) {
        // This is one half of configuring the action bar to have an up (back) arrow instead of
        // the usual hamburger menu. The other half is the override of configureActionBar().
        // I would like to put this line in that method but for some unknown reason that doesn't
        // work...we get no control at all in the hamburger position.

        filter = getIntent().getStringExtra("filter");

        // Initialize the bookshelf label (empty in MainActivity) with the data
        // passed through our intent.
        View toolbar = findViewById(;
        final int background = Color.parseColor("#" + getIntent().getStringExtra("background"));
        TextView labelText = (TextView) findViewById(;
        ImageView bloomIcon = (ImageView)findViewById(;
        // replace the main bloom icon with the bookshelf one.

        // The color chosen for the bookshelf may not contrast well with the default white
        // color of text and the back arrow and the default black color of the bookshelf icon.
        // Change them all to black or white, whichever gives better contrast.
        int forecolor = pickTextColorBasedOnBgColor(background, Color.WHITE, Color.BLACK);
        // This bit of magic from
        // makes the the back arrow use the contrasting foreground color
        Drawable upArrow = ((;
        upArrow.setColorFilter(forecolor, PorterDuff.Mode.SRC_ATOP);

        // And this bit, from,
        // switches the color of the bookshelf icon.

    // official W3C recommendation for deciding whether a light or dark color will contrast best
    // with the given background color. The right one to use is returned.
    // Based on
    int pickTextColorBasedOnBgColor(int bgColor, int lightColor, int darkColor) {
        int r = (bgColor >> 16) & 0xff;
        int g = (bgColor >> 8) & 0xff;
        int b = bgColor & 0xff;
        double[] uicolors = new double[] {r / 255, g / 255, b / 255};
        double[] c = new double[3];
        for (int i = 0; i < 3; i++) {
            double col = uicolors[i];
            if (col < 0.03928)
                c[i] = col/12.92;
                c[i] = Math.pow((col + 0.055)/1.055, 2.4);

        double luminance = (0.2126 * c[0]) + (0.7152 * c[1]) + (0.0722 * c[2]);
        return (luminance > 0.179) ? darkColor : lightColor;

    // In the shelf view the action bar's menu is replaced with a back button.
    // It's possible we could achieve this in another way, for example, by initializing the
    // shelf view with a layout that doesn't have a navigation bar at all. But as the views
    // are so similar, I like having them share the same layout.
    protected void configureActionBar(ActionBarDrawerToggle toggle) {
        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            public void onClick(View view) {

    protected void updateFilter() {

    protected BookCollection setupBookCollection() throws ExtStorageUnavailableException {
        if (BloomReaderApplication.theOneBookCollection == null) {
            // Presume the OS threw away and re-created the BloomReaderApplication while we
            // weren't looking. We need to do the full job of making one.
            return super.setupBookCollection();
        // normally we just want to use the one the main activity made.
        return BloomReaderApplication.theOneBookCollection;