package com.william.mangoreader.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.graphics.Palette;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.william.mangoreader.R;
import com.william.mangoreader.activity.MangaItemActivity;
import com.william.mangoreader.model.MangaEdenMangaDetailItem;

public class MangaItemDetailFragment extends Fragment {
    private static final String DESCRIPTION_FRAGMENT_KEY = "description_fragment_key";
    private static final String SWATCH_KEY = "swatch_key";
    private MangaEdenMangaDetailItem mangaDetailItem;

    private View rootView;
    private TextView titleView;
    private TextView subtitleView;
    private TextView descriptionView;

    private Palette.Swatch secondaryColor;

    public MangaItemDetailFragment() {
        //required empty constructor
    }

    public static MangaItemDetailFragment newInstance(MangaEdenMangaDetailItem mangaDetailItem) {
        MangaItemDetailFragment fragment = new MangaItemDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DESCRIPTION_FRAGMENT_KEY, mangaDetailItem);
        fragment.setArguments(bundle);

        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {

        mangaDetailItem = (MangaEdenMangaDetailItem) getArguments().getSerializable(DESCRIPTION_FRAGMENT_KEY);
        rootView = inflater.inflate(R.layout.fragment_details, container, false);

        titleView = (TextView) rootView.findViewById(R.id.manga_item_title);
        titleView.setText(mangaDetailItem.getTitle());

        subtitleView = (TextView) rootView.findViewById(R.id.manga_item_subtitle);
        subtitleView.setText(mangaDetailItem.getAuthor());

        descriptionView = (TextView) rootView.findViewById(R.id.manga_item_description);
        descriptionView.setText(mangaDetailItem.getDescription());

        setLayoutColors();

        return rootView;
    }

    public void setLayoutColors() {
        secondaryColor = ((MangaItemActivity) getActivity()).getSecondaryColor();

        rootView.setBackgroundColor(secondaryColor.getRgb());
        titleView.setTextColor(secondaryColor.getTitleTextColor());
        subtitleView.setTextColor(secondaryColor.getTitleTextColor());
        descriptionView.setTextColor(secondaryColor.getBodyTextColor());
    }
}

@@ -36,16 +36,47 @@
 gment;%0A%0A
+import android.graphics.Color;%0A
 import a
@@ -2475,24 +2475,25 @@
 Rgb());%0A
+%0A
         
 titleVie
@@ -2480,36 +2480,47 @@
 );%0A%0A        
-titleView.se
+int textColor = contras
 tTextColor(s
@@ -2528,37 +2528,61 @@
 condaryColor.get
-Title
+Rgb());%0A        titleView.set
 TextColor());%0A  
@@ -2567,33 +2567,41 @@
 ew.setTextColor(
-)
+textColor
 );%0A        subti
@@ -2625,40 +2625,17 @@
 lor(
-secondaryColor.getTitleT
+t
 extColor
 ());
@@ -2626,26 +2626,24 @@
 or(textColor
-()
 );%0A        d
@@ -2674,42 +2674,325 @@
 lor(
-secondaryColor.getBodyTextColor())
+textColor);%0A    %7D%0A%0A    public int contrastTextColor(int color) %7B%0A        // Contrast formula derived from http://stackoverflow.com/a/1855903/1222351%0A        double luminance = 0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color);%0A        return luminance %3E 127 ? Color.BLACK : Color.WHITE
 ;%0A  

Improve text color contrast in the manga detail page