import Velocity from 'velocity-animate'
import {
	PAGE_SCROLL_STARTED,
	PAGE_SCROLL_ENDED
} from '../constants'

const scrollEndpoints = ["content", "settings"]  // "home" is immovable
const scrollDuration = 1000
const scrollEasing = "ease-in-out"

export function scrollPage(endpoint, up) {

    if ( !scrollEndpoints.includes(endpoint) ) 
        throw new Error("Invalid scroll endpoint supplied to action");
    console.log("scrollPage");
    const offset = up ? 0 : window.innerHeight   

    return (dispatch, getState) => {
        console.log("shouldscroll?");
        if (up && !shouldScroll(getState(), endpoint)) return;
        console.log("yes");
        dispatch(startScroll(endpoint));
        return getTarget(endpoint).velocity({
            top: offset
        }, {
            duration: scrollDuration, 
            easing: scrollEasing, 
            complete: () => dispatch(endScroll(endpoint))
        });
    }
}

function shouldScroll({status}, target) {
    return status.currentPage !== target
}

function getTarget(endpoint) {
    const target = $('.page-'+endpoint) // will always be 1
    if (!target) {
        throw new Error(`No .page-${endpoint} found`);
    } else return target
}

function startScroll(currentPage) {
    return {
        type: PAGE_SCROLL_STARTED,
        payload: currentPage
    }
}

function endScroll(currentPage) {
    return {
        type: PAGE_SCROLL_ENDED,
        payload: currentPage
    }
}

export function scrollHeader(makeVisible) {
    const $header = $('#header'), duration=300;
    var delay=0, easing='ease-in', top=`-${$header.height()}px`;

    if (makeVisible) {
        delay = 700
        top = 0
        easing = 'ease-out'
    }

    return dispatch => {
        return $header.velocity({top}, {duration, easing, delay})   
    }
}
@@ -82,16 +82,35 @@
 LL_ENDED
+,%0A    SCROLL_HEADER
 %0A%7D from 
@@ -428,38 +428,8 @@
 %22);%0A
-    console.log(%22scrollPage%22);
 %0A   
@@ -435,22 +435,19 @@
   const 
-offset
+top
  = up ? 
@@ -476,83 +476,85 @@
    %0A
-%0A    return (dispatch, getState) =%3E %7B%0A        console.log(%22shouldscroll?%22);
+    const $target = getTarget(endpoint)%0A%0A    return (dispatch, getState) =%3E %7B
 %0A   
@@ -613,24 +613,25 @@
 return;%0A
+%0A
         
 console.
@@ -626,24 +626,30 @@
     
-console.log(%22yes
+$target.velocity(%22stop
 %22);%0A
@@ -689,16 +689,17 @@
 oint));%0A
+%0A
         
@@ -705,35 +705,23 @@
  return 
-getT
+$t
 arget
-(endpoint)
 .velocit
@@ -727,41 +727,11 @@
 ty(%7B
-%0A            top: offset%0A        
+top
 %7D, %7B
@@ -1432,27 +1432,32 @@
 lHeader(
-make
+to
 Visible
+, delay
 ) %7B%0A    
@@ -1512,16 +1512,43 @@
 var 
-delay=0,
+%0A        delay = delay%7C%7C0, %0A       
  eas
@@ -1562,16 +1562,25 @@
 se-in', 
+%0A        
 top=%60-$%7B
@@ -1614,12 +1614,10 @@
 if (
-make
+to
 Visi
@@ -1627,28 +1627,8 @@
 ) %7B%0A
-        delay = 700%0A
     
@@ -1674,16 +1674,17 @@
 %0A    %7D%0A%0A
+%0A
     retu
@@ -1686,24 +1686,25 @@
  return 
+(
 dispatch
  =%3E %7B%0A  
@@ -1695,16 +1695,27 @@
 dispatch
+, getState)
  =%3E %7B%0A  
@@ -1724,72 +1724,505 @@
     
-return $header.velocity(%7Btop%7D, %7Bduration, easing, delay%7D)   %0A    %7D%0A%7D
+if (shouldScrollHeader(getState(), toVisible))%7B%0A            $header.velocity(%22stop%22);%0A            dispatch(headerScroll(toVisible))%0A            return $header.velocity(%0A                %7Btop%7D, %0A                %7Bduration, easing, delay%7D%0A            )%0A        %7D%0A    %7D%0A%7D%0A%0Afunction headerScroll(toVisible) %7B%0A    return %7B%0A        type: SCROLL_HEADER,%0A        payload: toVisible%0A    %7D%0A%7D%0A%0Afunction shouldScrollHeader(%7Bstatus:%7B isHeaderVisible %7D%7D, toVisible) %7B    %0A    return isHeaderVisible !== toVisible %0A%7D%0A%0A

Add delay to scrollHeader action