import React from "react";
import classNames from "classnames";
import { makeStyles } from "@material-ui/core/styles";
import { QueryClient, QueryClientProvider, useQuery } from 'react-query'

import Header from "components/Header/Header.js";
import HeaderLinks from "components/Header/HeaderLinks.js";
import Footer from "components/Footer/Footer.js";
import Parallax from "components/Parallax/Parallax.js";
import styles from "assets/jss/nextjs-material-kit/pages/components.js";

import GetDataHelmet from "../../../api/product/helmet.js"
import DataHelmet from "../../../pages-sections/yamaha/product/helmet.js";
import DataSnackbarContent from "../../../pages-sections/yamaha/snackbar.js";
import Router from 'next/router'
import Cookies from "cookies";

const useStyles = makeStyles(styles);
const queryClient = new QueryClient();
const handleName = values => {
    Router.push({
        pathname: '/yamaha/product/helmet',
        query: (values&&values.length!=0)?{ filter: values[0]["name"] }:{},
    })
}
const Helmet = function ({ user, selected, helmet, mtx, extreme, ranger, backend, fighter, swirl, classic, uno, ...props }) {
    const classes = useStyles();
    const { ...rest } = props;
    return (
        <div>
        <Header
            rightLinks={<HeaderLinks username={user} />}
            fixed
            color="info"
            changeColorOnScroll={{
            height: 400,
            color: "white",
            }}
            {...rest}
        />
        <Parallax image={require("assets/img/yamalube.jpg")} height="200px"/>
        <div className={classNames(classes.main, classes.mainRaised)}>
            <QueryClientProvider client={queryClient}>
                <DataSnackbarContent/>
                <DataHelmet selected={(selected=="")?null:selected[0]["id"]} handleName={handleName} helmet={helmet} mtx={mtx} extreme={extreme} ranger={ranger} fighter={fighter} swirl={swirl} classic={classic} uno={uno} backend={backend} />
            </QueryClientProvider>
        </div>
        <Footer />
        </div>
    );
};

export default Helmet;

export async function getServerSideProps(context) {
    var helmet = [];
    var mtx = [];
    var extreme = [];
    var ranger = [];
    var fighter = [];
    var swirl = [];
    var classic = [];
    var uno = [];
    var filter = context.query.filter||"";
    var selected = "";

    const backend = process.env.BACKEND_SERVER_URI;
    
    var { req, resp } = context;
    const cookies = new Cookies(req, resp);
    var user = "";
    var userObj = (await cookies.get("user"))
        ? JSON.parse(await cookies.get("user"))
        : null;
    if (userObj) {
        let sessionId = userObj["partners_login_states"].filter(function (i) {
        return (
            i.business_partner && i.business_partner.name.toUpperCase() == "YAMAHA"
        );
        });
        if (sessionId.length != 0) user = userObj["username"];
    }

    var res = await GetDataHelmet.GetHelmetRConcepts(filter);
    if (res["STATUS"] === 1) {
        helmet = res["DATA"]["helmets"];
        if (filter !=""){
            selected = helmet.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetMTX(filter);
    if (res["STATUS"] === 1) {
        mtx = res["DATA"]["helmets"];
        if (filter !=""){
            selected = mtx.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetExtreme(filter);
    if (res["STATUS"] === 1) {
        extreme = res["DATA"]["helmets"];
        if (filter !=""){
            selected = extreme.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetRanger(filter);
    if (res["STATUS"] === 1) {
        ranger = res["DATA"]["helmets"];
        if (filter !=""){
            selected = ranger.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetFighter(filter);
    if (res["STATUS"] === 1) {
        fighter = res["DATA"]["helmets"];
        if (filter !=""){
            selected = fighter.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetSwirl(filter);
    if (res["STATUS"] === 1) {
        swirl = res["DATA"]["helmets"];
        if (filter !=""){
            selected = swirl.filter((i)=>i.name==filter);
        }
    }
    
    var res = await GetDataHelmet.GetHelmetClassic(filter);
    if (res["STATUS"] === 1) {
        classic = res["DATA"]["helmets"];
        if (filter !=""){
            selected = classic.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataHelmet.GetHelmetUno(filter);
    if (res["STATUS"] === 1) {
        uno = res["DATA"]["helmets"];
        if (filter !=""){
            selected = uno.filter((i)=>i.name==filter);
        }
    }

    return { 
        props: { user, selected, helmet, mtx, extreme, ranger, fighter, swirl, classic, uno, backend }, // will be passed to the page component as props
    };
}