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 SocialRoomHistory extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            roomHistory: [],
        };
    }

    async componentDidMount() {
        this.setState({ loading: true });
        try {
            this.setState({ roomHistory: [] });
            const res = await superagent.get(`/cloud/admin/account/${this.props.account.id}/social/history/room`).accept('json');
            const historyItems = res.body.items;
            const roomHistory = _.orderBy(historyItems, ["createdAt"], ["desc"]);
            this.setState({ roomHistory });
        } 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.roomHistory) {
            return (
                <Table celled striped compact>
                    <Table.Header>
                        <Table.Row>
                            <Table.HeaderCell width={1} collapsing>Date</Table.HeaderCell>
                            <Table.HeaderCell width={2} collapsing>Room</Table.HeaderCell>
                            <Table.HeaderCell width={2} collapsing>Type</Table.HeaderCell>
                            <Table.HeaderCell width={2} collapsing>Visibility</Table.HeaderCell>
                        </Table.Row>
                    </Table.Header>
                    <Table.Body>
                        {this.state.roomHistory.map(historyItem => {
                            return (
                            <Table.Row>
                                <Table.Cell width={1} collapsing>
                                    {formatInTimeZone(fromUnixTime(historyItem.createdAt / 1000), DEFAULT_TIMEZONE, 'yyyy-MM-dd HH:mm:ss zzz')}
                                </Table.Cell>
                                <Table.Cell width={2}>
                                    {historyItem.roomData ? historyItem.roomData.name : ""}
                                    {historyItem.room ? historyItem.room.name : ""}
                                </Table.Cell>
                                <Table.Cell width={2}>
                                    {historyItem.room.roomType}
                                </Table.Cell>
                                <Table.Cell width={2}>
                                    {historyItem.room.visibility}
                                </Table.Cell>
                            </Table.Row>
                            )
                        })}
    
                    </Table.Body>
                </Table>
            );
        } else {
            return null;
        }
    }
}
