package rest;

import java.io.File;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import sklep.db.DBConnection;
import sklep.db.DBException;
import sklep.db.ProductDAO;
import sklep.db.RecordNotFound;
import sklep.model.Price;
import sklep.model.Product;
import sklep.model.ProductList;
import sklep.photo.PhotoUtil;

// W tej klasie używany jest format XML (tylko nie dla zdjęć)
@Path("/products.xml")
@Produces("application/xml")
@Consumes("application/xml")
public class RProductsXml {
    @GET
    public ProductList readAll() throws DBException {
        try(DBConnection db = DBConnection.open()) {
            ProductDAO productDAO = db.productDAO();
            return new ProductList(productDAO.readAll());
        }
    }
    
    @GET
    @Path("/{id}")
    public Response readOne(@PathParam("id") int productId) {
        try(DBConnection db = DBConnection.open()) {
            ProductDAO productDAO = db.productDAO();
            Product product = productDAO.findById(productId);
            
            return Response.ok(product).build();
        } catch (DBException e) {
            String html = "<html><body><h1>Błąd!</h1><p style='color:red'>" + e + "</p></body></html>";
            
            return Response.serverError()
                    .type("text/html")
                    .entity(html)
                    .build();
                    
        } catch (RecordNotFound e) {
            String html = "<html><body><h1>Brak danych!</h1><p style='color:red'>" + e.getMessage() + "</p></body></html>";
            
            return Response.status(404)
                    .type("text/html")
                    .entity(html)
                    .build();
        }
    }
    
    @GET
    @Path("/{id}/price")
    public Price getPrice(@PathParam("id") int productId) throws DBException, RecordNotFound {
        try(DBConnection db = DBConnection.open()) {
            ProductDAO productDAO = db.productDAO();
            return new Price(productDAO.findById(productId).getPrice());
        }
    }
    
    @PUT
    @Path("/{id}/price")
    public void setPrice(
                @PathParam("id") int productId,
                Price newPrice
            ) throws DBException, RecordNotFound {
        try(DBConnection db = DBConnection.open()) {
            ProductDAO productDAO = db.productDAO();
            Product product = productDAO.findById(productId);
            product.setPrice(newPrice.getValue());
            productDAO.update(product);
            db.commit();
        }
    }
    
    // W tej wersji w wyniku zwracamy uzupełniony obiekt - będzie widać jakie ma ID.
    @POST
    public Product add(Product product) throws DBException {
        try(DBConnection db = DBConnection.open()) {
            ProductDAO productDAO = db.productDAO();
            productDAO.save(product);
            db.commit();
            return product;
        }
    }
    
    @GET
    @Path("/{id}/photo")
    @Produces("image/jpeg")
    public File photo(@PathParam("id") int productId) throws DBException, RecordNotFound {
        return PhotoUtil.getFile(productId);
    }
}
