package fr.imta.addressbook.server.handlers;

import fr.imta.addressbook.model.AddressBook;
import fr.imta.addressbook.model.Database;
import fr.imta.addressbook.model.Person;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

public class PersonHandler {
    private Database db;

    public PersonHandler(Database db) {
        this.db = db;
    }

    public void getIds(RoutingContext ctx) {
        ctx.json(db.getPersonIds());
    }

    public void getById(RoutingContext ctx) {
        int id = Integer.parseInt(ctx.pathParam("id"));
        Person person = db.getPersonById(id);

        if (person == null) {
            ctx.response().setStatusCode(404).end("Person not found");
        }
        else {
            ctx.json(person.toJSON());
        }
    }

    public void create(RoutingContext ctx) {
        JsonObject input = ctx.body().asJsonObject();

        if (!input.containsKey("firstname") || !input.containsKey("lastname")) {
            ctx.response().setStatusCode(500);
            ctx.json("firstname and lastname required");
        }
        else {
            Person p = db.createPerson(input.getString("firstname"), input.getString("lastname"));

            if (input.containsKey("addressbook")) {
                AddressBook a = db.getAddressBookById(input.getInteger("addressbook"));

                if (a == null) {
                    
                }
            }
    
            ctx.json(p.getId());
        }
    }

    public void update(RoutingContext ctx) {
        JsonObject input = ctx.body().asJsonObject();
        int id = Integer.parseInt(ctx.pathParam("id"));
        Person p = db.getPersonById(id);

        if (p == null) {
            ctx.response().setStatusCode(404);
            ctx.json("person not found");
        }
        else {
            if (input.containsKey("firstname")) {
                p.setFirstName(input.getString("firstname"));
            }
            if (input.containsKey("lastname")) {
                p.setLastName(input.getString("lastname"));
            }
            if (input.containsKey("addressbook")) {
                AddressBook addressBook = db.getAddressBookById(input.getInteger("addressbook"));
                if (addressBook == null) {
                    ctx.response().setStatusCode(500);
                    ctx.json("cannot find addressBook with id " + input.getInteger("addressbook"));
                    return;
                }
                else {
                    p.setAddressBook(addressBook);
                }
            }
            ctx.json("person updated successfully");
        }
    }

    public void delete(RoutingContext ctx) {
        int id = Integer.parseInt(ctx.pathParam("id"));
        Person p = db.getPersonById(id);

        if (p == null) {
            ctx.response().setStatusCode(404);
            ctx.json("person not found");
        }
        else {
            if (db.deletePerson(p)) {
                ctx.json("success");
            }
            else {
                ctx.response().setStatusCode(500);
                ctx.json("error deleting person " + p.getId());
            }
        }
    }
}
