package uk.co.pilllogger.animations;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;

import uk.co.pilllogger.R;
import uk.co.pilllogger.views.MyViewPager;

/**
 * Created by Nick on 07/12/13.
 */
public class FadeBackgroundPageTransformer implements ViewPager.PageTransformer {

    private static String TAG = "FadeBackgroundPageTransformer";
    private final MyViewPager _background;
    private final ActionBarActivity _activity;
    private final int _tabColour;
    float[] _transitionModifiers = new float[4];
    int _fadeFrom;
    int _fadeTo;
    ActionBar _actionBar;

    public FadeBackgroundPageTransformer(MyViewPager background, ActionBarActivity activity, int tabColour) {
        _background = background;
        _activity = activity;
        _tabColour = tabColour;
        _actionBar = _activity.getSupportActionBar();

        initTabs();

        // this is a little bit of a hack, if we ever go back to different coloured
        // backgrounds for pages, this will probably break things
        _background.setBackgroundColor(Color.WHITE);
    }

    private void initTabs(){
        setColorOfTab(0, 1);
        setColorOfTab(1, 0.25f);
        setColorOfTab(2, 0.25f);
    }

    @Override
    public void transformPage(View view, float position) {
        if(view == null)
            return;

        int colour = (Integer)view.getTag(R.id.tag_page_colour);
        int tabPosition = (Integer)view.getTag(R.id.tag_tab_icon_position);

        if(position <= 1 && position > -1){ // page is visible
            if(position < 0)
                _fadeTo = colour;
            else
                _fadeFrom = colour;

            if(position == 0) { // if page takes up full screen, both colours are equal
                _fadeTo = colour;
            }

            _transitionModifiers = calculateColourTransition(_fadeFrom, _fadeTo);

            float modifier = position * 100;

            float r = Color.red(_fadeFrom) - (modifier * _transitionModifiers[0]);
            float g = Color.green(_fadeFrom) - (modifier * _transitionModifiers[1]);
            float b = Color.blue(_fadeFrom) - (modifier * _transitionModifiers[2]);
            float a = Color.alpha(_fadeFrom) - (modifier * _transitionModifiers[3]);

            if(position > 0)
                _background.setBackgroundColor(Color.argb((int)a, (int)r, (int)g, (int)b));

            // transition tab bar icons
            if(_actionBar != null){
                float tabAlpha = 1 - (Math.abs(position) * 0.75f);

                setColorOfTab(tabPosition, tabAlpha);
            }
        }
        else {
            setColorOfTab(tabPosition, 0.25f);
        }
    }

    private void setColorOfTab(int tabPosition, float alpha){

        if(tabPosition >= _actionBar.getTabCount()){
            return;
        }

        ActionBar.Tab tab = _actionBar.getTabAt(tabPosition);
        View tabCustomView = tab.getCustomView();
        ImageView tabImage = (ImageView) tabCustomView.findViewById(R.id.tab_icon_image);
        Drawable background = tabImage.getDrawable();

        background.mutate().setColorFilter(_tabColour, PorterDuff.Mode.MULTIPLY);

        if(alpha >= 0)
            tabImage.setAlpha(alpha);
    }

    private float[] calculateColourTransition(int from, int to) {
        float[] results = new float[4];
        int[] fadeFrom = { Color.red(from), Color.green(from), Color.blue(from), Color.alpha(from)};
        int[] fadeTo = { Color.red(to), Color.green(to), Color.blue(to), Color.alpha(to)};

        for (int i = 0 ; i < fadeFrom.length ; i++) {
            float diff = fadeFrom[i] - fadeTo[i];
            results[i] = diff/100.0f;
        }
        return results;
    }
}

@@ -2662,48 +2662,8 @@
 ons%0A
-            if(_actionBar != null)%7B%0A    
     
@@ -2721,21 +2721,16 @@
 0.75f);%0A
-%0A    
         
@@ -2771,30 +2771,16 @@
 Alpha);%0A
-            %7D%0A
         
@@ -2922,17 +2922,16 @@
 alpha)%7B%0A
-%0A
         
@@ -2933,16 +2933,38 @@
      if(
+_actionBar == null %7C%7C 
 tabPosit

Fix crash when the actionbar isn't available