package com.moac.android.palettedemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
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.ImageView;
import android.widget.Toast;

import java.util.Observable;
import java.util.Observer;

public class ImageFragment extends Fragment implements Observer {

    private static final String RESOURCE_ID_ARG = "RESOURCE_ID_ARG";
    private static final String PALETTE_OPTION_ARG = "PALETTE_OPTION_ARG";

    private ImageView imageView;
    private FragmentContainer fragmentContainer;

    public static ImageFragment create(int resourceId, PaletteOption option) {
        ImageFragment fragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putInt(RESOURCE_ID_ARG, resourceId);
        args.putSerializable(PALETTE_OPTION_ARG, option);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            fragmentContainer = (FragmentContainer) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement FragmentContainer");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fragmentContainer = null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_image, container, false);

        int imageResourceId = getArguments().getInt(RESOURCE_ID_ARG);
        imageView = (ImageView) root.findViewById(R.id.imageView_pic);
        imageView.setImageResource(imageResourceId);

        return root;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        PaletteOption initialOption = (PaletteOption) getArguments().getSerializable(PALETTE_OPTION_ARG);
        onChangePaletteOption(initialOption);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPaletteObservable().addObserver(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPaletteObservable().deleteObserver(this);
    }

    @Override
    public void update(Observable observable, Object data) {
        onChangePaletteOption((PaletteOption) data);
    }

    private void onChangePaletteOption(PaletteOption paletteOption) {

        if (getView() == null) return;

        Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        Palette palette = Palette.from(bmp).generate();
        Palette.Swatch swatch;

        switch (paletteOption) {
            case VIBRANT:
                swatch = palette.getVibrantSwatch();
                break;
            case VIBRANT_DARK:
                swatch = palette.getDarkVibrantSwatch();
                break;
            case VIBRANT_LIGHT:
                swatch = palette.getLightVibrantSwatch();
                break;
            case MUTED:
                swatch = palette.getMutedSwatch();
                break;
            case MUTED_DARK:
                swatch = palette.getDarkMutedSwatch();
                break;
            case MUTED_LIGHT:
                swatch = palette.getLightVibrantSwatch();
                break;
            default:
                swatch = palette.getVibrantSwatch();
        }
        if (swatch != null) {
            getView().setBackgroundColor(swatch.getRgb());
        } else {
            // This could get awkward if multiple images show this simultaneously!
            Toast.makeText(getActivity(), "No swatch for " + palette, Toast.LENGTH_SHORT).show();
        }
    }

    private Observable getPaletteObservable() {
        return fragmentContainer.getPaletteObservable();
    }

    interface FragmentContainer {
        Observable getPaletteObservable();
    }

}
@@ -252,16 +252,41 @@
 alette;%0A
+import android.util.Log;%0A
 import a
@@ -553,24 +553,100 @@
 Observer %7B%0A%0A
+    private static final String TAG = ImageFragment.class.getSimpleName();%0A%0A
     private 
@@ -734,16 +734,20 @@
  String 
+KEY_
 PALETTE_
@@ -752,20 +752,16 @@
 E_OPTION
-_ARG
  = %22PALE
@@ -770,20 +770,16 @@
 E_OPTION
-_ARG
 %22;%0A%0A    
@@ -855,16 +855,57 @@
 ntainer;
+%0A    private PaletteOption paletteOption;
 %0A%0A    pu
@@ -1138,32 +1138,36 @@
 putSerializable(
+KEY_
 PALETTE_OPTION_A
@@ -1164,20 +1164,16 @@
 E_OPTION
-_ARG
 , option
@@ -2272,24 +2272,107 @@
 tion initial
+PaletteOption;%0A        if (savedInstanceState == null) %7B%0A            initialPalette
 Option = (Pa
@@ -2415,16 +2415,20 @@
 lizable(
+KEY_
 PALETTE_
@@ -2433,22 +2433,152 @@
 E_OPTION
-_ARG);
+);%0A        %7D else %7B%0A            initialPaletteOption = (PaletteOption) savedInstanceState.getSerializable(KEY_PALETTE_OPTION);%0A        %7D
 %0A       
@@ -2607,16 +2607,23 @@
 (initial
+Palette
 Option);
@@ -2875,32 +2875,218 @@
 r(this);%0A    %7D%0A%0A
+    @Override public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putSerializable(KEY_PALETTE_OPTION, paletteOption);%0A    %7D%0A%0A
     @Override%0A  
@@ -3271,24 +3271,135 @@
 eOption) %7B%0A%0A
+        Log.d(TAG, %22onChangePaletteOption(): %22 + paletteOption);%0A%0A        this.paletteOption = paletteOption;%0A%0A
         if (

Save and restore PaletteOption on configuration change