import React from "react"
import { Component } from "react"
import Plot from "react-plotly.js"

import { NewTickData, socket } from '../';


type FirstConnectData = {
    ticks: Array<number>,
    prices: Array<number>
}

type PlotState = {
    x: Array<number>,
    y: Array<number>
}

class RPlot extends Component<{}, PlotState> {
    constructor(props) {
        super(props)
    }

    state = {
        x: [],
        y: []
    }

    componentDidMount() {
        socket.on("first_connect", (data: FirstConnectData) => {
            this.setState({
                x: data.ticks,
                y: data.prices
            })
        })

        socket.on("new_tick", (data: NewTickData) => {
            this.setState({
                x: [...this.state.x, data.tick],
                y: [...this.state.y, data.price],
            })
        })
    }

    render() {
        return (
            <Plot
                data={[
                    {
                        x: this.state.x,
                        y: this.state.y,
                        type: 'scatter',
                        mode: 'lines+markers',
                        "marker.colorbar": { tickformat: ".1f"}
                    },
                ]}
                layout={{ dragmode: "pan", autosize: true }}
                config={{
                    scrollZoom: true,
                    displayModeBar: false,
                    responsive: true,
                }}
                style={{ width: '90%', height: '90%' }}
            />
        )
    }
}

export default RPlot;
