import express from 'express';
import http from 'http';
import cors from 'cors';
import mysql from 'mysql';
import { v1 } from 'uuid';

const port: number = 443;
const app: express.Express = express();
const httpServer: http.Server = http.createServer(app);
const tournaments: Record<string, number> = {};
const players: Record<string, number> = {};
const connection = mysql.createPool({
  connectionLimit: 10,
  host: 'test-db.csmrqtei38qi.ap-northeast-1.rds.amazonaws.com',
  user: 'admin',
  password: '12341234',
  database: 'catherine_league'
});
connection.query(
  `SELECT id, tournament_key
    FROM tournaments`,
  (error: mysql.MysqlError | null, results: any) => {
    if (error) {
      return;
    }
    if (results.length > 0) {
      results.forEach((result: any) => {
        tournaments[result.tournament_key] = result.id;
      });
    }
  }
);
connection.query(
  `SELECT id, player_key FROM players;`,
  (error: mysql.MysqlError | null, results: any) => {
    if (error) {
      console.log(error);
      return;
    }
    if (results.length > 0) {
      results.forEach((result: any) => {
        players[result.player_key] = result.id;
      });
    }
  }
);
if (process.env.NODE_ENV === 'development') {
  app.use(cors());
  app.options('*', cors());
}

app.get('/api/players/:key', (req: any, res: any) => {
  if (req.params && req.params.key && tournaments[req.params.key]) {
    const sqlToPrepare = `SELECT p.id, p.player_key, p.player_name,p.description,p.image,c.name_id, c.name, c.name_jp
    FROM players p
    JOIN player_to_tournament ptt
      on p.id = ptt.player_id
    JOIN characters c
      on c.id = p.character_id
    where ptt.tournament_id = ?`;
    const sql = mysql.format(sqlToPrepare, [tournaments[req.params.key]]);
    connection.query(sql,
      (error: mysql.MysqlError | null, results: any) => {
        if (error) {
          console.error(error);
          // players not found
        } else {
          res.send(results);
        }
      }
    );
  } else {
    // send error
  }
});
app.get('/api/players', (req: any, res: any) => {
  connection.query(
    `SELECT p.id, p.player_key, p.player_name,p.description,p.image,c.name_id, c.name, c.name_jp
      FROM players p
      JOIN characters c
        on c.id = p.character_id`,
    (error: mysql.MysqlError | null, results: any) => {
      if (error) {
        console.log(error);
        // players not found
      } else {
        res.send(results);
      }
    }
  );
});
app.get('/api/guide', (req: any, res: any) => {
  connection.query(
    `SELECT *
      FROM guide`,
    (error: mysql.MysqlError | null, results: any) => {
      if (error) {
        console.log(error);
        res.send([]);
      } else {
        res.send(results);
      }
    }
  );
});
app.get('/api/archive', (req: any, res: any) => {
  connection.query(
    `SELECT *
      FROM archive`,
    (error: mysql.MysqlError | null, results: any) => {
      if (error) {
        console.log(error);
        res.send([]);
      } else {
        res.send(results);
      }
    }
  );
});
app.get('/api/contact', (req: any, res: any) => {
  connection.query(
    `SELECT *
      FROM contact`,
    (error: mysql.MysqlError | null, results: any) => {
      if (error) {
        console.log(error);
        res.send([]);
      } else {
        res.send(results);
      }
    }
  );
});

app.use('/players', express.static('client'));
app.use('/tournaments*', express.static('client'));
app.use('/about', express.static('client'));
app.use('/guide', express.static('client'));
app.use('/archive', express.static('client'));
app.use('/contact', express.static('client'));
app.use(express.static('client'));

httpServer.listen(port, () => {
  console.log(`listening to port ${port}`);
});
