import React from 'react';
import superagent from 'superagent';

import { fromUnixTime } from "date-fns";
import { formatInTimeZone } from 'date-fns-tz'

import { Table, Segment } from 'semantic-ui-react'
import { DEFAULT_TIMEZONE } from '../../CloudApi/Constants.js';

export default class SocialNotifications extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            notifications: [],
        };
    }

    async componentDidMount() {
        this.setState({ loading: true });
        try {
            this.setState({ notifications: [] });
            const res = await superagent.get(`/cloud/admin/account/${this.props.account.id}/social/notifications`).accept('json');
            console.log(res.body.items);
            this.setState({ notifications: res.body.items });
        } catch (e) {
            console.log(e);
        }
        this.setState({ loading: false });
    }

    render() {
        if (this.state.loading) {
            return <Segment basic loading={this.state.loading}>&nbsp;</Segment>
        }

        if (this.state.notifications) {
            const notifications = _.orderBy(this.state.notifications, ["createdAt"], ["desc"]);
            return (
                <div>
                    <Table celled striped compact>
                        <Table.Header>
                            <Table.Row>
                                <Table.HeaderCell width={1} collapsing>Date</Table.HeaderCell>
                                <Table.HeaderCell width={1} collapsing>Sender</Table.HeaderCell>
                                <Table.HeaderCell width={1} collapsing>Type</Table.HeaderCell>
                                <Table.HeaderCell width={1} collapsing>Status</Table.HeaderCell>
                                <Table.HeaderCell width={4}>Title</Table.HeaderCell>
                            </Table.Row>
                        </Table.Header>
                        <Table.Body>
                            {notifications.map(notification => {
                                return (
                                    <Table.Row>
                                        <Table.Cell width={1} collapsing>
                                            {formatInTimeZone(fromUnixTime(notification.createdAt / 1000), DEFAULT_TIMEZONE, 'yyyy-MM-dd HH:mm:ss zzz')}
                                        </Table.Cell>
                                        <Table.Cell width={1}>
                                            <a href={`/account/${notification.senderBigscreenAccountId}`}>{notification.senderBigscreenAccountId}</a>
                                        </Table.Cell>
                                        <Table.Cell width={1}>
                                            {notification.notificationType}
                                        </Table.Cell>
                                        <Table.Cell width={1}>
                                            {notification.notificationStatus}
                                        </Table.Cell>
                                        <Table.Cell width={4}>
                                            {notification.title}
                                        </Table.Cell>
                                    </Table.Row>
                                )
                            })}
                        </Table.Body>
                    </Table>
                </div>
            );
        } else {
            return null;
        }
    }
}
