import React from "react";
import { Button, InputGroup, FormControl } from "react-bootstrap";
import "../styles/Dashboard.css";
import { apiClient } from "../utils/httpClients.js";
import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay";
import SportInfoCardDisplay from "../components/SportInfoCardDisplay";
import { globalContext } from "../context";
import { needUser } from "../utils/routing";

export default class Dashboard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            displayedMatches: [],
            displayedSports: [],
            displayedEquipment: [],
            user: null
        };
    }

    static contextType = globalContext;

    async componentDidMount() {
        await needUser(this.context.navigate);
        await this.latestMatches();
        await this.availableSports();
    }
    async latestMatches() {
        let recentMatchesRes = await apiClient.get("/match/recent/15");
        if (recentMatchesRes.status === 200) {
            this.setState({ displayedMatches: recentMatchesRes.data.recent });
        }
    }

    async availableSports() {
        let availableSportsRes = await apiClient.get("/sport");
        if (availableSportsRes.status === 200) {
            this.setState({ displayedSports: availableSportsRes.data });
        }
    }

    render() {
        return (
            <React.Fragment>
                <h1></h1>
                <InputGroup className="w-50">
                    <FormControl
                        placeholder="Search for Matches"
                        aria-label="Search Bar"
                        aria-describedby="basic-addon2"
                    />
                    <Button variant="outline-secondary" id="button-addon2">
                        Search
                    </Button>
                </InputGroup>
                <div className="p-4">
                    <h2>Available Matches</h2>
                    <MatchInfoCardDisplay recommendedmatches={this.state.displayedMatches} />
                </div>
                <div className="p-4">
                    <h2>Available Sports</h2>
                    <SportInfoCardDisplay recommendedsports={this.state.displayedSports} />
                </div>


            </React.Fragment>
        );
    }
}