import React, {Component} from "react";
import {Col, Container, Navbar, Row} from "react-bootstrap";
import HCard from "./HCard";
import RPlot from "./RPlot";
import {FirstConnectMessage, NewTickMessage, PositionState, socket} from "../";
import {PositionTable} from "./Tables";
import {EventData, Events} from "./Events";

type AppState = {
    current_price: number,
    current_tick: number,
    last_update: Date,
    positions: Array<PositionState>,
    events: Array<EventData>
}


class App extends Component<{}, AppState> {
    state = {
        current_price: 0,
        current_tick: 0,
        last_update: new Date(),
        positions: [],
        events: []
    }

    constructor(props) {
        super(props)
    }

    componentDidMount() {
        socket.on("first_connect", (data: FirstConnectMessage) => {
            console.log(data.positions)
            this.setState({
                current_price: data.prices[data.prices.length - 1],
                current_tick: data.ticks[data.ticks.length - 1],
                last_update: new Date(),
                positions: data.positions
            })
        })

        socket.on("new_tick", (data: NewTickMessage) => {
            const event: EventData = {name: "New Tick", id: data.tick, tick: data.tick}

            this.setState({
                current_price: data.price,
                current_tick: data.tick,
                last_update: new Date(),
                positions: data.positions,
                events: [...this.state.events, event]
            })
        })
    }

    render() {
        return (
            <div className="d-flex flex-fill flex-column">
                <Navbar bg="light" expand="lg" className="border-bottom">
                    <Navbar.Brand href="#">Rustico - BfxBot</Navbar.Brand>
                </Navbar>

                <Container fluid className="mt-2 border flex-fill d-flex">
                    <Row className="flex-fill">
                        {/* Toolbar column */}
                        <Col md={4} lg={4} className="border-right d-flex flex-column">
                            <Row className="justify-content-center border-bottom py-2">
                                <HCard title="Current tick:" content={String(this.state.current_tick)}
                                       update={this.state.last_update}/>
                                <HCard title="Current price:" content={String(this.state.current_price)}
                                       update={this.state.last_update}/>
                            </Row>
                            <Row className={"flex-fill"}>
                                {this.state.positions ? <PositionTable positions={this.state.positions}/> : null}
                            </Row>
                            <Row className={"my-2"}>
                                {this.state.events ? <Events events={this.state.events}/> : null}
                            </Row>
                        </Col>

                        {/* Graph column */}
                        <Col md={8} lg={8} className="flex-fill">
                            <RPlot/>
                        </Col>
                    </Row>
                    {/* <Row className="flex-grow-1 border-top border-bottom">
                        <RPlot />
                    </Row> */}
                </Container>
                <footer className="footer py-3 bg-light border-top">
                    <Container className="text-center">
                        <span className="text-muted">Made with ❤️ by the Pepe</span>
                    </Container>
                </footer>
            </div>
        )
    }
}

export default App;