package us.faramita.encore.dao;

import java.sql.Timestamp;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import org.jetbrains.annotations.NotNull;
import us.faramita.encore.db.*;

/**
 * Created by 4faramita on 2017/5/22.
 */
public class Search {
    private int id;
    private int type;

    public Search(int id) {
        this.id = id;
    }

    Task getInfo() {
        TaskEntity task;

        Object object = new Object();

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        String queryTask = "from TaskEntity task where task.id = ?";
        task = (TaskEntity) session.createQuery(queryTask)
                .setParameter(0, this.id)
                .getSingleResult();
        this.type = task.getType();
        boolean achieved = (task.getAchieved() == 1);

        if (type == 0) {  // Timer
            TimerEntity timer;

            String query = "from TimerEntity timer where timer.taskId = ?";
            timer = (TimerEntity) session.createQuery(query)
                    .setParameter(0, this.id)
                    .getSingleResult();

            object = getObject(task.getId(), task.getName(), task.getDescription(), timer.getPeriod(), achieved, task.getExedTimes());
        } else if (type == 1) {  // TwE
            TaskWithEndingEntity taskWithEndingEntity;

            String query = "from TaskWithEndingEntity taskWithEndingEntity where taskWithEndingEntity.taskId = ?";
            taskWithEndingEntity = (TaskWithEndingEntity) session.createQuery(query)
                    .setParameter(0, this.id)
                    .getSingleResult();

            object = getObject(task.getId(), task.getName(), task.getDescription(), taskWithEndingEntity.getEnding(), taskWithEndingEntity.getAutoRepeat(), taskWithEndingEntity.getDone(), achieved, task.getExedTimes());
        } else if (type == 2) {  // TwP
            TaskWithPeriodEntity taskWithPeriodEntity;

            String query = "from TaskWithPeriodEntity taskWithPeriodEntity where taskWithPeriodEntity.taskId = ?";
            taskWithPeriodEntity = (TaskWithPeriodEntity) session.createQuery(query)
                    .setParameter(0, this.id)
                    .getSingleResult();

            object = getObject(task.getId(), task.getName(), task.getDescription(), taskWithPeriodEntity.getPeriod(), taskWithPeriodEntity.getAutoRepeat(), taskWithPeriodEntity.getDone(), taskWithPeriodEntity.getOpen(), achieved, task.getExedTimes());
        }

        session.getTransaction().commit();
        session.close();

        return (Task) object;
    }

    @NotNull
    private Timer getObject(int id, String name, String description, int period, boolean achieved, int exedTimes) {
        return new Timer(id, name, description, period, achieved, exedTimes);
    }

    @NotNull
    private TaskWithEnding getObject(int id, String name, String description, Timestamp ending, byte autoRepeat, byte done, boolean achieved, int exedTimes) {
//        boolean isDone = (done == 1);

        return new TaskWithEnding(id, name, description, ending, (int) autoRepeat, (done == 1), achieved, exedTimes);
    }

    @NotNull
    private TaskWithPeriod getObject(int id, String name, String description, int period, byte autoRepeat, byte done, byte open, boolean achieved, int exedTimes) {
        boolean repeat = (autoRepeat == 1);
        boolean isDone = (done == 1);
        boolean isOpen = (open == 1);

        return new TaskWithPeriod(id, name, description, period, repeat, isDone, isOpen, achieved, exedTimes);
    }

    public static void main(String[] args) {
        Search search = new Search(20);
        TaskWithPeriod task = (TaskWithPeriod) search.getInfo();
        System.out.println(task.getId());
//        int id = timer.deleteTask();
//        System.out.println(id);
    }
}
