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

const scrollTargets = ["board", "thread", "settings"]  // "home" is immovable

export function scrollPage({ page, direction, callback, to="home", duration=1000, easing='ease-in-out' }) {

    if ( !scrollTargets.includes(page) ) 
        throw new Error(`Invalid page supplied to scrollPage: ${page}; ${scrollTargets}`);

    const top = direction === "up" ? 0 : window.innerHeight   
    const $target = getTarget(page)

    return (dispatch, getState) => {
        if (shouldntScroll(getState(), page, direction)) 
            return

        if (direction !== "up") {
            // Fix scroll toggle. Change state when reverting
            page = to
        }

        $target.velocity("stop");
        dispatch(startScroll());

        return $target.velocity({top}, {
            duration, easing,
            complete: () => {
                dispatch(endScroll(page))
                if (callback) callback();
            }
        });
    }
}

function shouldntScroll({status}, target, direction) {
    return status.currentPage === target && direction === "up"
}

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() {
    return {
        type: PAGE_SCROLL_STARTED,
    }
}

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

// TODO: Destructure scrollHeader args
export function scrollHeader(toVisible, delay) {
    const $header = $('#header'), duration=300;
    var 
        delay = delay||0, 
        easing='ease-in', 
        top=`-${$header.height()}px`;

    if (toVisible) {
        top = 0
        easing = 'ease-out'
    }


    return (dispatch, getState) => {
        if (shouldScrollHeader(getState(), toVisible)){
            $header.velocity("stop");
            dispatch(headerScroll(toVisible))
            return $header.velocity(
                {top}, 
                {duration, easing, delay}
            )
        }
    }
}

function headerScroll(toVisible) {
    console.log("Action headerScroll: " + toVisible)
    return {
        type: SCROLL_HEADER,
        toVisible
    }
}

function shouldScrollHeader({status:{ isHeaderVisible }}, toVisible) {    
    return isHeaderVisible !== toVisible 
}


@@ -88,29 +88,30 @@
 ED,%0A    
-SCROLL_HEADER
+HEADER_TOGGLED
 %0A%7D from 
@@ -2014,16 +2014,17 @@
 ible))%7B%0A
+%0A
         
@@ -2076,30 +2076,30 @@
 patch(header
-Scroll
+Toggle
 (toVisible))
@@ -2095,24 +2095,25 @@
 toVisible))%0A
+%0A
             
@@ -2160,33 +2160,16 @@
  %7Btop%7D, 
-%0A                
 %7Bduratio
@@ -2234,22 +2234,22 @@
 n header
-Scroll
+Toggle
 (toVisib
@@ -2284,22 +2284,22 @@
 n header
-Scroll
+Toggle
 : %22 + to
@@ -2338,21 +2338,22 @@
 pe: 
-SCROLL_HEADER
+HEADER_TOGGLED
 ,%0A  
@@ -2430,16 +2430,29 @@
 rVisible
+, currentPage
  %7D%7D, toV
@@ -2506,12 +2506,37 @@
 Visible 
+&& currentPage !== %22home%22
 %0A%7D%0A%0A

Make header scroll check current page, rename methods