import {Toast} from "native-base";
<template>
    <nb-container class="container">
        <image-background class="splash-image" :source="splashImage" />

        <nb-content class="logo-wrapper">
            <logo class="logo"/>
        </nb-content>

        <nb-text class="app-name">Pocket Diary</nb-text>

        <nb-content class="greetings-wrapper">
            <nb-h1 class="greetings-text">{{ greetingsText }}, {{ userObj.name }}!</nb-h1>
        </nb-content>

        <nb-content class="spinner-wrapper">
            <nb-spinner class="spinner" color="#fff"/>
        </nb-content>
    </nb-container>
</template>

<script>
    import { StatusBar } from "react-native";
    import { Toast } from "native-base";

    import Logo from "../components/Logo"

    import greetings_morning from "../../assets/greetings-splashes/greetings-morning.jpg";
    import greetings_day from "../../assets/greetings-splashes/greetings-day.jpg";
    import greetings_evening from "../../assets/greetings-splashes/greetings-evening.jpg";
    import greetings_night from "../../assets/greetings-splashes/greetings-night.jpg";

    import store from "../store";

    export default {
        name: "Greetings",
        components: {
            Logo
        },
        props: {
            navigation: { type: Object }
        },
        data() {
            return {
                splashImage: null,
                greetingsText: "",
                userObj: Object,
                action: String,
            }
        },
        mounted(animation) {
            StatusBar.setHidden(true, animation);
            this.sayHello();
            this.userObj = this.navigation.getParam('userObj');
            this.action = this.navigation.getParam('action');
            this.getUserGroup();
            (this.action === 'set_user') ? this.setUser() : this.authenticate();
        },
        methods: {
            sayHello() {
                let currentDate = new Date(),
                    hours = currentDate.getHours();
                console.log(hours);

                if (hours >= 6 && hours <= 10) {
                    this.greetingsText = "Доброе утро";
                    this.splashImage = greetings_morning
                } else if (hours >= 11 && hours <= 16) {
                    this.greetingsText = "Добрый день";
                    this.splashImage = greetings_day
                } else if (hours >= 17 && hours <= 21) {
                    this.greetingsText = "Добрый вечер";
                    this.splashImage = greetings_evening
                } else {
                    this.greetingsText = "Доброй ночи";
                    this.splashImage = greetings_night
                }

                console.log(this.greetings)
            },
            getUserGroup() {
                if (this.userObj.group === 1) {
                    this.userGroup = 'user';
                } else if (this.userObj.group === 2) {
                    this.userGroup = 'moder';
                } else {
                    this.userGroup = 'admin';
                }
            },
            setUser() {
                store.dispatch('SET_USER', { userObj: this.userObj, userGroup: this.userGroup }).then(() => this.loadLessons())
            },
            authenticate() {
                store.dispatch('LOGIN', {
                    userObj: this.userObj,
                    userGroup: this.userGroup,
                    navigate: this.navigation.navigate
                }).then(() => this.loadLessons());
            },
            loadLessons() {
                this.$plugins.f('lesson', 'GET', store.state.userObj.api_token).then(response => {
                    if (response.body.status) {
                        store.dispatch('SET_LESSONS', { lessons: response.body.schedule }).then(() => this.loadCalls());
                    } else {
                        Toast.show({
                            text: "Произошла непредвиденная ошибка при загрузке",
                            buttonText: "Закрыть",
                            type: "danger",
                            duration: 5000
                        });
                    }
                });
            },
            loadCalls() {
                this.$plugins.f('call', 'GET', store.state.userObj.api_token).then(response => {
                    if (response.body.status) {
                        store.dispatch('SET_CALLS', { calls: response.body.schedule }).then(() => this.navigation.navigate('Drawer'));
                    } else {
                        Toast.show({
                            text: "Произошла непредвиденная ошибка при загрузке",
                            buttonText: "Закрыть",
                            type: "danger",
                            duration: 5000
                        });
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .splash-image {
        flex: 1;
    }

    .logo-wrapper {
        position: absolute;
        bottom: 50px;
        width: 85px;
        height: 110px;
        align-self: center;
        padding: 5px;
        background-color: #fff;
        border-top-left-radius: 2px;
        border-top-right-radius: 10px;
        border-bottom-left-radius: 2px;
        border-bottom-right-radius: 10px;
    }

    .logo {
        width: 75px;
        height: 100px;
        align-self: center;
    }

    .app-name {
        position: absolute;
        bottom: 175px;
        left: 0;
        width: 100%;
        font-size: 26px;
        color: #fff;
        text-align: center;
    }

    .greetings-wrapper {
        position: absolute;
        top: 150px;
        left: 0;
        width: 100%;
        z-index: 1;
    }

    .greetings-text {
        color: #fff;
        text-align: center;
    }

    .spinner-wrapper {
        position: absolute;
        top: 50%;
        width: 100%;
    }

    .spinner {
        align-self: center;
    }
</style>