/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.examples.pas.swingui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.optaplanner.core.impl.solution.Solution;
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.examples.common.swingui.TangoColorFactory;
import org.optaplanner.examples.pas.domain.Bed;
import org.optaplanner.examples.pas.domain.BedDesignation;
import org.optaplanner.examples.pas.domain.Night;
import org.optaplanner.examples.pas.domain.PatientAdmissionSchedule;
import org.optaplanner.examples.pas.solver.move.BedChangeMove;

/**
 * TODO this code is highly unoptimized
 */
public class PatientAdmissionSchedulePanel extends SolutionPanel {

    private static final Color HEADER_COLOR = TangoColorFactory.BUTTER_1;

    private GridLayout gridLayout;

    public PatientAdmissionSchedulePanel() {
        gridLayout = new GridLayout(0, 1);
        setLayout(gridLayout);
    }

    private PatientAdmissionSchedule getPatientAdmissionSchedule() {
        return (PatientAdmissionSchedule) solutionBusiness.getSolution();
    }

    public void resetPanel(Solution solution) {
        removeAll();
        PatientAdmissionSchedule patientAdmissionSchedule = (PatientAdmissionSchedule) solution;
        gridLayout.setColumns(patientAdmissionSchedule.getNightList().size() + 1);
        JLabel headerCornerLabel = new JLabel("Department_Room_Bed  \\  Night");
        headerCornerLabel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.DARK_GRAY),
                BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        headerCornerLabel.setBackground(HEADER_COLOR);
        headerCornerLabel.setOpaque(true);
        add(headerCornerLabel);
        for (Night night : patientAdmissionSchedule.getNightList()) {
            JLabel nightLabel = new JLabel(night.toString());
            nightLabel.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(Color.DARK_GRAY),
                    BorderFactory.createEmptyBorder(2, 2, 2, 2)));
            nightLabel.setBackground(HEADER_COLOR);
            nightLabel.setOpaque(true);
            add(nightLabel);
        }
        Map<Bed, Map<Night, BedNightPanel>> bedNightPanelMap = new HashMap<Bed, Map<Night, BedNightPanel>>();
        for (Bed bed : patientAdmissionSchedule.getBedList()) {
            createBedLine(patientAdmissionSchedule, bedNightPanelMap, bed);
        }
        createBedLine(patientAdmissionSchedule, bedNightPanelMap, null);
        for (BedDesignation bedDesignation : patientAdmissionSchedule.getBedDesignationList()) {
            for (Night night : patientAdmissionSchedule.getNightList()) {
                if (bedDesignation.getAdmissionPart().getFirstNight().getIndex() <= night.getIndex()
                        && night.getIndex() <= bedDesignation.getAdmissionPart().getLastNight().getIndex()) {
                    BedNightPanel bedNightPanel = bedNightPanelMap.get(bedDesignation.getBed()).get(night);
                    bedNightPanel.addBedDesignation(bedDesignation);
                }
            }
        }
    }

    private void createBedLine(PatientAdmissionSchedule patientAdmissionSchedule,
            Map<Bed, Map<Night, BedNightPanel>> bedNightPanelMap, Bed bed) {
        JLabel bedLabel = new JLabel(bed == null ? "Unassigned" : bed.toString());
        bedLabel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.DARK_GRAY),
                BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        bedLabel.setBackground(HEADER_COLOR);
        bedLabel.setOpaque(true);
        add(bedLabel);
        Map<Night, BedNightPanel> nightPanelMap = new HashMap<Night, BedNightPanel>();
        bedNightPanelMap.put(bed, nightPanelMap);
        for (Night night : patientAdmissionSchedule.getNightList()) {
            BedNightPanel bedNightPanel = new BedNightPanel();
            add(bedNightPanel);
            nightPanelMap.put(night, bedNightPanel);
        }
    }

    private class BedNightPanel extends JPanel {

        public BedNightPanel() {
            super(new GridLayout(0, 1));
            setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(Color.DARK_GRAY),
                    BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        }

        public void addBedDesignation(BedDesignation bedDesignation) {
            JButton button = new JButton(new BedDesignationAction(bedDesignation));
            add(button);
        }

    }

    private class BedDesignationAction extends AbstractAction {

        private BedDesignation bedDesignation;

        public BedDesignationAction(BedDesignation bedDesignation) {
            super(bedDesignation.getAdmissionPart().getPatient().getName());
            this.bedDesignation = bedDesignation;
        }

        public void actionPerformed(ActionEvent e) {
            JPanel listFieldsPanel = new JPanel(new GridLayout(2, 1));
            List<Bed> bedList = getPatientAdmissionSchedule().getBedList();
            JComboBox bedListField = new JComboBox(bedList.toArray());
            bedListField.setSelectedItem(bedDesignation.getBed());
            listFieldsPanel.add(bedListField);
            int result = JOptionPane.showConfirmDialog(PatientAdmissionSchedulePanel.this.getRootPane(), listFieldsPanel,
                    "Select bed", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                Bed toBed = (Bed) bedListField.getSelectedItem();
                solutionBusiness.doMove(new BedChangeMove(bedDesignation, toBed));
                solverAndPersistenceFrame.resetScreen();
            }
        }

    }

}
