/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import service.CampeonatoService;
import controller.DetalheCampeonatoController;
import controller.PrincipalController;
import java.awt.Window;
import service.Sessao;
import service.Mensagem;
import javax.swing.JOptionPane;
import util.helper.DateTimeHelper;

/**
 *
 * @author Hellen
 */
public class FRMDetalheCampeonato extends javax.swing.JInternalFrame {

    private final DetalheCampeonatoController controller;

    public FRMDetalheCampeonato() {
        initComponents();
        this.setSize(PrincipalController.FRM_SIZE.width / 2, PrincipalController.FRM_SIZE.height / 2);
        controller = new DetalheCampeonatoController();
        preencherCampos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanelCabecalho = new javax.swing.JPanel();
        jLabelTitulo = new javax.swing.JLabel();
        jLabelTituloEdit = new javax.swing.JLabel();
        jPanelConteudo = new javax.swing.JPanel();
        jLabelNome = new javax.swing.JLabel();
        jLabelDataInicio = new javax.swing.JLabel();
        jLabelDescricao = new javax.swing.JLabel();
        jLabelFase = new javax.swing.JLabel();
        jLabelFaseEdit = new javax.swing.JLabel();
        jLabelNomeEdit = new javax.swing.JLabel();
        jLabelDataInicioEdit = new javax.swing.JLabel();
        jLabelDescricaoEdit = new javax.swing.JLabel();
        jLabelStatus = new javax.swing.JLabel();
        jLabelStatusEdit = new javax.swing.JLabel();
        jPanelOpcoes = new javax.swing.JPanel();
        jButtonEditar = new javax.swing.JButton();
        jButtonFinalizar = new javax.swing.JButton();
        jButtonReativar = new javax.swing.JButton();
        jPanelRodape = new javax.swing.JPanel();
        jButtonFechar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Detalhes do Campeonato");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanelCabecalho.setBackground(new java.awt.Color(0, 102, 153));
        jPanelCabecalho.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabelTitulo.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabelTitulo.setForeground(new java.awt.Color(255, 255, 255));
        jLabelTitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clipboard-icon.png"))); // NOI18N
        jLabelTitulo.setText("Informações - ");
        jPanelCabecalho.add(jLabelTitulo);

        jLabelTituloEdit.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabelTituloEdit.setForeground(new java.awt.Color(255, 255, 255));
        jLabelTituloEdit.setText("Campeonato");
        jPanelCabecalho.add(jLabelTituloEdit);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jPanelCabecalho, gridBagConstraints);

        jPanelConteudo.setLayout(new java.awt.GridBagLayout());

        jLabelNome.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelNome.setText("Nome:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelNome, gridBagConstraints);

        jLabelDataInicio.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelDataInicio.setText("Início:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelDataInicio, gridBagConstraints);

        jLabelDescricao.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelDescricao.setText("Descrição:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelDescricao, gridBagConstraints);

        jLabelFase.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelFase.setText("Fase:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelFase, gridBagConstraints);

        jLabelFaseEdit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelFaseEdit.setText("Fase Atual");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelFaseEdit, gridBagConstraints);

        jLabelNomeEdit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelNomeEdit.setText("nome");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelNomeEdit, gridBagConstraints);

        jLabelDataInicioEdit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelDataInicioEdit.setText("data");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelDataInicioEdit, gridBagConstraints);

        jLabelDescricaoEdit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelDescricaoEdit.setText("descricao");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelDescricaoEdit, gridBagConstraints);

        jLabelStatus.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelStatus.setText("Status:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelStatus, gridBagConstraints);

        jLabelStatusEdit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelStatusEdit.setText("status");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelConteudo.add(jLabelStatusEdit, gridBagConstraints);

        jPanelOpcoes.setLayout(new java.awt.GridBagLayout());

        jButtonEditar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit-2-icon.png"))); // NOI18N
        jButtonEditar.setText("Editar");
        jButtonEditar.setToolTipText("Editar dados do campeonato");
        jButtonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelOpcoes.add(jButtonEditar, gridBagConstraints);

        jButtonFinalizar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonFinalizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/attention-icon.png"))); // NOI18N
        jButtonFinalizar.setText("Finalizar");
        jButtonFinalizar.setToolTipText("Finalizar campeonato. O campeonato será finalizado, não será possível fazer o controle de equipes e jogos. A ação pode ser revertida nessa mesma tela.");
        jButtonFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFinalizarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelOpcoes.add(jButtonFinalizar, gridBagConstraints);
        jButtonFinalizar.setVisible(false);

        jButtonReativar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonReativar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/attention-icon.png"))); // NOI18N
        jButtonReativar.setText("Reativar");
        jButtonReativar.setToolTipText("Reativar campeonato. Campeonato será reativado, será possível realizar o controle de equipes e jogos.");
        jButtonReativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonReativarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanelOpcoes.add(jButtonReativar, gridBagConstraints);
        jButtonReativar.setVisible(false);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        jPanelConteudo.add(jPanelOpcoes, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jPanelConteudo, gridBagConstraints);

        jPanelRodape.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jButtonFechar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel-icon.png"))); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });
        jPanelRodape.add(jButtonFechar);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jPanelRodape, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarActionPerformed
        FRMCadastroCampeonato fRMCadastroCampeonato = new FRMCadastroCampeonato(null, true, Sessao.getCampeonatoAtual());
        fRMCadastroCampeonato.setLocationRelativeTo(null);
        fRMCadastroCampeonato.setVisible(true);
        controller.atualizarCampeonato();
        preencherCampos();
    }//GEN-LAST:event_jButtonEditarActionPerformed

    private void jButtonFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFinalizarActionPerformed
        switch (JOptionPane.showConfirmDialog(this, "Deseja finalizar o campeonato?", "Finalizar Campeonato", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) {
            case JOptionPane.YES_OPTION:
                if (controller.finalizarCampeonato()) {
                    JOptionPane.showMessageDialog(this, Mensagem.feedback);
                    reabrir();
                }
                break;
            default:
                break;
        }
    }//GEN-LAST:event_jButtonFinalizarActionPerformed

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void jButtonReativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReativarActionPerformed
        switch (JOptionPane.showConfirmDialog(this, "Deseja reativar o campeonato?", "Reativar Campeonato", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) {
            case JOptionPane.YES_OPTION:
                if (controller.reativarCampeonato()) {
                    JOptionPane.showMessageDialog(this, Mensagem.feedback);
                    reabrir();
                }
                break;
            default:
                break;
        }
    }//GEN-LAST:event_jButtonReativarActionPerformed

    private void preencherCampos() {
        this.jLabelNomeEdit.setText(Sessao.getCampeonatoAtual().getNome());
        this.jLabelDataInicioEdit.setText(DateTimeHelper.dataBancoParaJava(Sessao.getCampeonatoAtual().getDataInicio()));
        this.jLabelStatusEdit.setText(Sessao.getCampeonatoAtual().getStatus() == CampeonatoService.ATIVO ? "Em andamento" : "Finalizado");
        this.jLabelFaseEdit.setText(Sessao.getCampeonatoAtual().getFase().getNome());
        this.jLabelDescricaoEdit.setText(Sessao.getCampeonatoAtual().getObservacoes());
        if (CampeonatoService.ATIVO == Sessao.getCampeonatoAtual().getStatus()) {
            this.jButtonFinalizar.setVisible(true);
        } else {
            this.jButtonReativar.setVisible(true);
        }
    }

    private void reabrir() {
        System.gc();
        for (Window window : Window.getWindows()) {
            window.dispose();
        }
        new FRMPrincipal().setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JButton jButtonFinalizar;
    private javax.swing.JButton jButtonReativar;
    private javax.swing.JLabel jLabelDataInicio;
    private javax.swing.JLabel jLabelDataInicioEdit;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelDescricaoEdit;
    private javax.swing.JLabel jLabelFase;
    private javax.swing.JLabel jLabelFaseEdit;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelNomeEdit;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JLabel jLabelStatusEdit;
    private javax.swing.JLabel jLabelTitulo;
    private javax.swing.JLabel jLabelTituloEdit;
    private javax.swing.JPanel jPanelCabecalho;
    private javax.swing.JPanel jPanelConteudo;
    private javax.swing.JPanel jPanelOpcoes;
    private javax.swing.JPanel jPanelRodape;
    // End of variables declaration//GEN-END:variables
}
