
// Core imports
import { Component, OnInit } from '@angular/core'
import { trigger, state, style, transition, animate } from '@angular/animations'

// Libs
import { Subscription } from 'rxjs/Subscription'

// Core Services


// Module Services
import { SendCartService } from '../../services/svc.send-cart'

// Interfaces
import { ICartMessage } from '../../interfaces/cart-message'


/**
 * @name MessageComponent
 * @description Subscribes to messages and displays them with optional action
 * @author Charles Gouldmann - Klean
 * @since Thu Apr 11 2019
 * @extends
 */
@Component({
    moduleId: module.id+ '',
    selector: 'send-cart-message',
    templateUrl: './template/t--message.pug',
    styleUrls: ['sty.message.scss'],
    animations: [
        trigger('message', [
            state('none', style({ opacity: 0, transform: 'scale(0)' })),
            state('show', style({ opacity: 1, transform: 'scale(1)' })),

            transition('none => show', [
                animate('300ms ease-out', style({ transform: "scale(1.2)" })),
                animate('150ms ease-in')
            ]),

            transition('show => none', [
                animate('200ms ease-in', style({ transform: "scale(1.05)" })),
                animate('100ms ease-out')
            ])
        ])
    ]
})

export class MessageComponent {

    // ---- Variables ---- \\
    messages: ICartMessage[]

    // Subscriptions
    private subscriptions = new Subscription()




    constructor(
        private _sendCart: SendCartService
    ) {
        this.subscriptions.add(
            _sendCart.cartMessage$.subscribe((res) => {
                this.messages = res
            })
        )
    }



    // ---- Lifecycle hooks ---- \\
    ngOnInit() {
        // test
        //this._sendCart._addMessage('error', 'the session has been lost', {type: 'button', label: 'please go back to the cart', url: '/kurv'})
    }

    ngOnDestroy () {
        this.subscriptions.unsubscribe()
    }


    // ---- Functions ---- \\

    /**
     * Go to given location with a page-refresh
     * @param url 
     */
    goTo(url: string) {
        if(!!window) {
            window.location.href = url
        }
    }

}
