package br.com.elotech.websaude.odontograma.backend.model;

import br.com.elotech.websaude.odontograma.ui.Application;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import static br.com.elotech.websaude.odontograma.backend.model.dao.Conexao.CONN;

public class OdontoTratamentoModel {

    static PreparedStatement pstmt;
    static String SQL = "";

    public ResultSet listaTratamentos(Integer usuCodigo) throws SQLException {

        SQL = "SELECT odt.odo_trat_codigo,odt.odo_trat_dtinicial,odt.odo_trat_dtinicial_tratamento,odt.odo_trat_dtfinal,odt.odo_trat_dtfinal_exame,odt.odo_trat_status,usr.usr_nome FROM odonto_tratamento odt " +
                "INNER JOIN atendimento ate ON odt.ate_codigo_origem=ate.ate_codigo " +
                "LEFT JOIN usuarios usr ON usr.usr_codigo=ate.med_codigo " +
                "WHERE ate.usu_codigo = ? ORDER BY odt.odo_trat_codigo DESC";
        pstmt = CONN.prepareStatement(SQL);
        pstmt.setInt(1,usuCodigo);
        System.out.println(pstmt);
        return pstmt.executeQuery();
    }


    public int salvarTratamentoAction() throws SQLException {

        ResultSet rs = new AtendimentoModel().buscaRetornoOrigem(null);
        int ateCodOrigem = 0;
        if(rs.next())
            ateCodOrigem = rs.getInt("ate_codigo");

        SQL = "INSERT INTO odonto_tratamento(odo_trat_dtinicial,odo_trat_status,ate_codigo_origem) VALUES (?,?,?)";
        pstmt = CONN.prepareStatement(SQL);
        pstmt.setTimestamp(1,new Timestamp(new Date().getTime()));
        pstmt.setString(2,"A");
        pstmt.setInt(3,ateCodOrigem);
        pstmt.executeUpdate();
        CONN.commit();

        SQL = "SELECT odo_trat_codigo FROM odonto_tratamento WHERE ate_codigo_origem=? ORDER BY odo_trat_codigo DESC LIMIT 1";
        pstmt = CONN.prepareStatement(SQL);
        pstmt.setInt(1,ateCodOrigem);
        rs = pstmt.executeQuery();

        if(rs.next()){
            SQL = "INSERT INTO odonto_procedimentos_controle(odo_trat_codigo,ate_codigo) VALUES(?,?)";
            pstmt = CONN.prepareStatement(SQL);
            pstmt.setInt(1,rs.getInt("odo_trat_codigo"));
            pstmt.setInt(2,ateCodOrigem);
            pstmt.executeUpdate();
            CONN.commit();

            return rs.getInt("odo_trat_codigo");
        }
        return 0;
    }

    public ResultSet getQtdAtendimentoFaltanteTratamento(Integer tratCodigo) throws SQLException {

        SQL = "SELECT count(odp.odo_proc_codigo) as qtd_atendimento FROM odonto_procedimentos odp " +
                "INNER JOIN odonto_procedimentos_controle odpc ON odp.odo_pcon_codigo=odpc.odo_pcon_codigo " +
                "INNER JOIN odonto_tratamento odt ON odpc.odo_trat_codigo = odt.odo_trat_codigo " +
                "INNER JOIN atendimento ate ON odt.ate_codigo_origem=ate.ate_codigo " +
                "WHERE dp.odo_proc_status = 'F' AND odt.odo_trat_codigo =?";
        pstmt = CONN.prepareStatement(SQL);
        pstmt.setInt(1,tratCodigo);
        return pstmt.executeQuery();
    }

    public void finalizarTratamento(int tratCodigo) throws SQLException {
        SQL = "UPDATE odonto_tratamento SET odo_trat_dtfinal=now(), odo_trat_status='F' WHERE odo_trat_codigo="+tratCodigo;
        CONN.prepareStatement(SQL).executeUpdate();
        CONN.commit();
    }

    public void reabrir(Long codigo) throws SQLException {
        SQL = "UPDATE odonto_tratamento SET odo_trat_dtfinal=null, odo_trat_status='A' WHERE odo_trat_codigo="+codigo;
        CONN.prepareStatement(SQL).executeUpdate();
        CONN.commit();
    }

    public int abrir() throws SQLException {
        SQL = "INSERT INTO odonto_tratamento(odo_trat_status,odo_trat_dtinicial,ate_codigo_origem) VALUES('A',now(),"+Application.args[7]+")";
        CONN.prepareStatement(SQL).executeUpdate();
        CONN.commit();
        SQL = "SELECT odo_trat_codigo FROM odonto_tratamento WHERE ate_codigo_origem="+Application.args[7]+" AND odo_trat_status='A' LIMIT 1";
        ResultSet rs = CONN.prepareStatement(SQL).executeQuery();
        if(rs.next()) {

            SQL = "INSERT INTO odonto_procedimentos_controle(odo_trat_codigo,ate_codigo) VALUES(?,?)";
            pstmt = CONN.prepareStatement(SQL);
            pstmt.setInt(1,rs.getInt("odo_trat_codigo"));
            pstmt.setInt(2,Integer.valueOf(Application.args[7]));
            pstmt.executeUpdate();
            CONN.commit();

            return rs.getInt("odo_trat_codigo");
        }
        return 0;
    }

    public String getAnotacao(Long tratCodigo) throws SQLException {

        String anotacao = "";

        SQL = "SELECT odo_anotacao FROM odonto_tratamento WHERE odo_trat_codigo="+tratCodigo;
        ResultSet rs = CONN.prepareStatement(SQL).executeQuery();
        if(rs.next()){
            if(rs.getString("odo_anotacao")!=null)
                return rs.getString("odo_anotacao");
        }
        return anotacao;
    }



    public void setAnotacao(Long tratCodigo, String anotacao) throws SQLException {

        SQL = "UPDATE odonto_tratamento SET odo_anotacao=?  WHERE odo_trat_codigo="+tratCodigo;
        pstmt = CONN.prepareStatement(SQL);
        pstmt.setString(1,anotacao);
        pstmt.executeUpdate();
        CONN.commit();
    }
}
