package com.github.averyregier.club.broker;

import com.github.averyregier.club.db.tables.records.CeremonyRecord;
import com.github.averyregier.club.domain.club.Ceremony;
import org.jooq.DSLContext;
import org.jooq.TableField;

import java.time.LocalDate;
import java.util.Map;
import java.util.Optional;

import static com.github.averyregier.club.db.tables.Ceremony.CEREMONY;

/**
 * Created by avery on 3/2/15.
 */
public class CeremonyBroker extends Broker<Ceremony> {
    public CeremonyBroker(Connector connector) {
        super(connector);
    }

    @Override
    protected void persist(Ceremony ceremony, DSLContext create) {
        if(create.insertInto(CEREMONY)
                .set(CEREMONY.ID, ceremony.getId().getBytes())
                .set(mapFields(ceremony))
                .onDuplicateKeyUpdate()
                .set(mapFields(ceremony))
                .execute() != 1) {
            fail("Ceremony persistence failed: " + ceremony.getId());
        }

    }

    private Map<TableField<CeremonyRecord, ?>, Object> mapFields(Ceremony ceremony) {
        return JooqUtil.<CeremonyRecord>map()
                .set(CEREMONY.PRESENTATION_DATE, ceremony.presentationDate() != null ?
                        new java.sql.Date(ceremony.presentationDate().toEpochDay()):
                        null)
                .set(CEREMONY.NAME, ceremony.getName())
                .build();
    }

    public Optional<Ceremony> find(String ceremonyId) {
        return query(create->{
            CeremonyRecord record = create.selectFrom(CEREMONY).where(CEREMONY.ID.eq(ceremonyId.getBytes())).fetchOne();
            if(record == null) return Optional.empty();
            LocalDate date = LocalDate.ofEpochDay(record.getPresentationDate().getTime());
            String name = record.getName();
            return Optional.of(new Ceremony() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getId() {
                    return ceremonyId;
                }

                @Override
                public String getShortCode() {
                    return getName();
                }

                @Override
                public LocalDate presentationDate() {
                    return date;
                }
            });
        });
    }
}

@@ -297,16 +297,52 @@
 ptional;
+%0Aimport java.util.function.Function;
 %0A%0Aimport
@@ -1504,29 +1504,64 @@
     
-return query(
+Function%3CDSLContext, Optional%3CCeremony%3E%3E fn = 
 create
+ 
 -%3E
+ 
 %7B%0A  
@@ -1693,16 +1693,17 @@
       if
+ 
 (record 
@@ -2446,23 +2446,90 @@
 );%0A        %7D
-)
+;%0A        Optional%3CCeremony%3E query = query(fn);%0A        return query
 ;%0A    %7D%0A%7D%0A

fix build error