"use strict";
// const express = require('express');
// const router = express.Router();
// import { Address } from "../entity/Address";
// import {Student} from "../entity/Student";
// import { Client } from "../entity/Client";
// import { ClientType } from "../entity/ClientType";
// import { TypeStudent } from "../entity/TypeStudent";
// import { isUndefined } from "util";
// import { createConnection } from "typeorm";
// var cors = require('cors')
// //router.options('*', cors()); 
// if(process.env.NODE_ENV == "test"){
//     collection = createConnection("test")
// }
// else if(process.env.NODE_ENV == "production"){
//     collection = createConnection("production")
// }else{
//     collection = createConnection("production")
// }
// router.use(function timeLog(req, res, next) {
//     console.log('Time: ', Date.now());
//     next();
//   });
// router.get('/clients', function (req, res) {
//     collection.then(async connection => {
//         let clientRepository = connection.getRepository(Client);
//         console.log("Loading users from the database...");
//         let clients = await clientRepository.find({ relations: ["Students","ClientType","Addresses","Students.TypeStudent","Students.TitleStudent","Classes","Classes.ClassesDetails","Classes.ClassesDetails.Course"] });
//         res.send(clients);
//     }).catch(error => console.log(error));
// });
// router.get('/clienttypes', function (req, res) {
//     collection.then(async connection => {
//         let ClientTypeRepository = connection.getRepository(ClientType);
//         console.log("Loading users from the database...");
//         let clientTypes = await ClientTypeRepository.find();
//         res.send(clientTypes);
//     }).catch(error => console.log(error));
// });
// router.get('/client/:id', function (req, res) {
//     collection.then(async connection => {
//         let clientRepository = connection.getRepository(Client);
//         console.log("Loading users from the database...");
//         const client = await clientRepository.findOne(req.params.id, { relations: ["Students","ClientType","Addresses","Students.TypeStudent","Students.TitleStudent","Students.Client"
//         ,"Classes","Classes.TeachingMode","Classes.ClassesDetails","Classes.ClassesDetails.Students","Classes.ClassesDetails.Course"] });
//         res.send(client);
//         console.log(client)
//     }).catch(error => console.log(error));
// });
// router.put('/updateclient/:id',cors(), function (req, res) { 
//     console.log('updateclient')
//     console.log(req.body)
//      collection.then(async connection => {
//          const clientRepository = connection.getRepository(Client);
//          const client = await clientRepository.findOne(req.params.id, { relations: ["Students","Students.TypeStudent","Students.TitleStudent"] });
//          clientRepository.merge(client, req.body);
//          const results = await clientRepository.save(client);
//          const addressRepository = connection.getRepository(Address);
//          req.body.Addresses.forEach(async a => {
//              console.log(a)
//             if(isUndefined(a.id) || a.id == null){
//                 const AddressRepository = connection.getRepository(Address);
//                 const address : Address = await AddressRepository.create(a);
//                 address.Client = results;
//                 await AddressRepository.save(address);
//             }else{
//                 const address = await addressRepository.findOne(a.id)
//                 addressRepository.merge(address,a);
//                 await addressRepository.save(address);
//             }
//          })
//          res.send(results);
//      }).catch(error => console.log(error));
//  });
//  router.delete('/deleteclient/:id',cors(), function (req, res) {
//     console.log(req.body);
//      collection.then(async connection => {
//          const clientRepository = connection.getRepository(Client);
//          const client = await clientRepository.findOne(req.params.id, { relations: ["Students","Students.TypeStudent","Students.TitleStudent"] });
//          client.enable = false;
//          const results = await clientRepository.save(client);
//          res.send(results);
//      }).catch(error => console.log(error));
//  });
// router.post('/addclient',cors(), function (req, res) {
//    console.log(req.body);
//     collection.then(async connection => {
//         const clientRepository = connection.getRepository(Client);
//         const StudentRepository = connection.getRepository(Student);
//         const AdressRepository = connection.getRepository(Address);
//         const client = await clientRepository.create(req.body);
//         const results  = await clientRepository.save(client);
//         console.log(results)
//         let TypeStudentRepository = connection.getRepository(TypeStudent);
//         let typeStudent = await TypeStudentRepository.findOne({ where: { name: "Provider"}});
//         console.log(typeStudent)
//         client.Students = results.Students;
//         client.Students[0].Client = new Client();
//         client.Students[0].Client.id = results.id ;
//         client.Students[0].TypeStudent = new TypeStudent();
//         client.Students[0].TypeStudent.id = typeStudent.id;
//         client.Addresses = results.Addresses;
//         client.Addresses[0].Client = new Client();
//         client.Addresses[0].Client.id = results.id ;
//         let s = await StudentRepository.save(client.Students);
//         let a = await AdressRepository.save(client.Addresses);
//         //let results2  = await clientRepository.save(client);
//         console.log(results)
//         res.send(results);
//     }).catch(error => console.log(error));
// });
// router.post('/addStudent',cors(), function (req, res) {
//     console.log(req.body);
//      collection.then(async connection => {
//          const StudentRepository = connection.getRepository(Student);
//          const student = await StudentRepository.create(req.body);
//          const results = await StudentRepository.save(student);
//          res.send(results);
//      }).catch(error => console.log(error));
//  });
//  router.put('/updateStudent/:id',cors(), function (req, res) {
//     console.log(req.body);
//     collection.then(async connection => {
//         const StudentRepository = connection.getRepository(Student);
//         const student = await StudentRepository.findOne(req.params.id, { relations: ["TypeStudent","TitleStudent"] });
//         StudentRepository.merge(student, req.body);
//         const results = await StudentRepository.save(student);
//         res.send(results);
//     }).catch(error => console.log(error));
//  });
//  router.delete('/deletestudent/:id', function (req, res) {
//     console.log('deletestudent')
//     console.log(req.params.id)
//     collection.then(async connection => {
//         const StudentRepository = connection.getRepository(Student);
//         let s : Student =  await StudentRepository.findOne(req.params.id);
//         s.enable = s.enable==false?true:false;
//         await StudentRepository.save(s);        
//         res.send(true);
//     }).catch(error => res.send(error));
// });
// router.post('/addaddress',cors(), function (req, res) {
//     console.log(req.body);
//      collection.then(async connection => {
//          const AddressRepository = connection.getRepository(Address);
//          const address = await AddressRepository.create(req.body);
//          const results = await AddressRepository.save(address);
//          res.send(results);
//      }).catch(error => console.log(error));
//  });
//  router.put('/updateaddresses',cors(), function (req, res) {
//     console.log("updateaddresses");
//     console.log(req.body);
//     collection.then(async connection => {
//         const AddressRepository = connection.getRepository(Address);
//         let Addresses : Array<Address> = req.body;
//         console.log(Addresses);
//         Addresses.forEach(async a => {
//             console.log(a);
//             const address = await AddressRepository.findOne(a.id);
//             AddressRepository.merge(address, a);
//             await AddressRepository.save(address);
//         })
//         res.send(true);
//     }).catch(error => console.log(error));
//  });
//  module.exports = router ;
