package controller.note;

import controller.Controller;
import controller.history.Application_Controller;
import org.sig.fw.database.model.DBModel;
import org.sig.fw.db.bddc.model.Dossier;
import org.sig.fw.db.corrida.dao.Commande_DBAO;
import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.Note_DBAO;
import org.sig.fw.db.corrida.dao.Relance_History_DBAO;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.Note;
import org.sig.fw.db.corrida.model.Relance_History;
import org.sig.fw.sig.model.Account;
import org.sig.fw.utility.date.SDate;
import view.note.Notes_View;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Notes_Controller extends Controller {
	
	private Notes_View vw_notes = null;
	private Dossier dossier = null;
	private Thread update_thread = null;
	private Note update_note = null;
	
	private List<Note> notes = new ArrayList<Note>();
	
	public Notes_Controller(Application_Controller ctrl_application, Dossier dossier) {
		super(ctrl_application);
		this.dossier = dossier;
		this.vw_notes = new Notes_View(this);
		this.vw_notes.setVisible(true);
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				update_thread = new Thread(new Runnable() {
					@Override
					public void run() {
						while(true) {
							updateNotes();
							try {
								Thread.sleep(30000);
							} catch (InterruptedException e) {
								e.printStackTrace();
								break;
							}
						}
					}
				});
				update_thread.start();
			}
		});
	}
	
	public Dossier getDossier() {
		return this.dossier;
	}
	
	public void setUpdateNote(Note note) {
		this.update_note = note;
		this.vw_notes.setUpdateText(note.getNote());
	}
	
	public Notes_View getView() {
		return this.vw_notes;
	}
	
	public void stop() {
		this.update_thread.interrupt();
	}
	
	public void updateNotes() {
		List<Note> newNotes = new Note_DBAO("n").getWithCategoryAndReference(Note_DBAO.CATEGORY_DOSSIER, this.dossier);
		newNotes.addAll(this.getRelance());
		Collections.sort(newNotes, Collections.reverseOrder());
		if(!this.notes.equals(newNotes)) {
			this.notes = newNotes;
			this.vw_notes.updateNotes(this.notes);
		}
	}
	
	private List<Note> getRelance() {
		List<Note> notes = new ArrayList<Note>();
		
		for(Commande com : new Commande_DBAO("com").getWithDossier(this.dossier)) {
			for(Facture fact : new Facture_DBAO("fact").getWithCommande(com)) {
				for(Relance_History rh : new Relance_History_DBAO("rh").getWithObject(1, fact)) {
					if(!rh.getObservation().startsWith("Report de ")) {
						notes.add(this.parseRhToNote(rh));
					}
				}
				for(Relance_History rh : new Relance_History_DBAO("rh").getWithObject(2, fact)) {
					if(!rh.getObservation().startsWith("Report de ")) {
						notes.add(this.parseRhToNote(rh));
					}
				}
			}
		}
		return notes;
	}
	
	private Note parseRhToNote(Relance_History rh) {
		int note_id = -1;
		String category = "";
		DBModel reference = null;
		Account write_by = rh.getAccount();
		SDate write_date = rh.getActionDate();
		String note = "Relance par " + rh.getMode().getName() + " : " + rh.getObservation();
		return new Note(note_id, category, reference, write_by, write_date, note);
	}
	
	public void sendMessage(String text) {
		Note_DBAO dbao_note = new Note_DBAO("n");
		if(this.update_note == null) {
			Note note = new Note(-1, Note_DBAO.CATEGORY_DOSSIER, this.dossier, this.ctrl_application.getConnectedAccount(), new SDate(), text);
			dbao_note.insert(note);
		} else {
			this.update_note.setNote(text);
			dbao_note.update(update_note);
			this.update_note = null;
		}
		this.updateNotes();
	}
	
	public void deleteNote(Note note) {
		Note_DBAO dbao_note = new Note_DBAO("n");
		dbao_note.delete(note);
		this.update_note = null;
		this.updateNotes();
		this.vw_notes.setUpdateText("");
	}
	
}
