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

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

import { Table, Segment, Button, Modal, Header, Message, Icon } from 'semantic-ui-react'
import { DEFAULT_TIMEZONE } from '../../CloudApi/Constants.js';

export default class SocialFriendsList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            friends: [],
            pendingGraphItemId: null,
            showDialog: false
        };
    }

    async componentDidMount() {
        return this.reload();
    }

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

    onShowDialog(pendingGraphItemId, e) {
        e.preventDefault();
        this.setState({showDialog: true, pendingGraphItemId });
    }

    closeDialog(e) {
        this.setState({showDialog: false, pendingGraphItemId: null });
    }

    async onConfirmDeleteBlockEntry(e) {
        e.preventDefault();

        try {
            const result = await superagent.delete(`/cloud/admin/social/graph/${this.state.pendingGraphItemId}`).accept('json');
            this.setState({ showDialog: false, pendingGraphItemId: null });
            await this.reload();
        } catch (e) {
            this.setState({ showDialog: false, pendingGraphItemId: null });
            console.log(e);
        }
    }

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

        if (this.state.friends) {
            const friends = _.orderBy(this.state.friends, ["createdAt"], ["desc"]);
            return (
                <div>
                    <Table celled striped compact>
                        <Table.Header>
                            <Table.Row>
                                <Table.HeaderCell width={1} collapsing>Date</Table.HeaderCell>
                                <Table.HeaderCell width={2} collapsing>Target Account</Table.HeaderCell>
                            </Table.Row>
                        </Table.Header>
                        <Table.Body>
                            {friends.map(friend => {
                                return (
                                    <Table.Row>
                                        <Table.Cell width={1} collapsing>
                                            {formatInTimeZone(fromUnixTime(friend.createdAt / 1000), DEFAULT_TIMEZONE, 'yyyy-MM-dd HH:mm:ss zzz')}
                                        </Table.Cell>
                                        <Table.Cell width={2}>
                                            <a href={`/account/${friend.targetBigscreenAccountId}`}>{friend.targetSocialProfileSnapshot.username}</a>
                                        </Table.Cell>
                                    </Table.Row>
                                )
                            })}
                        </Table.Body>
                    </Table>
                </div>
            );
        } else {
            return null;
        }
    }
}
