import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
//-----------------------------------------------
public class InformationPanel extends JPanel{
    private static final String path = "./resources/Posters/";
    private JButton     movieBtn, backBtn;
    private JLabel      nameLabel, yearLabel, countryLabel, langLabel, duratLabel, directLabel, actorLabel, writerLabel,
                        ratingLabel, scoreLabel,  metaLabel,  pathLabel;
    private JTextArea   sumLabel;
    private Movie       movie;
    private JPanel      cards;
    //-----------------------
    public InformationPanel() {
        JLabel          nameHead, yearHead, countryHead, langHead, duratHead, directHead, actorHead, writerHead,
                        ratingHead, scoreHead, metaHead, pathHead, sumHead;
        GroupLayout     layout = new GroupLayout(this);
        //---------
        movieBtn = new JButton();
        backBtn = new JButton();
        nameHead = new JLabel();        nameLabel = new JLabel();
        yearHead = new JLabel();        yearLabel = new JLabel();
        countryHead = new JLabel();     countryLabel = new JLabel();
        langHead = new JLabel();        langLabel = new JLabel();
        duratHead = new JLabel();       duratLabel = new JLabel();
        directHead = new JLabel();      directLabel = new JLabel();
        actorHead = new JLabel();       actorLabel = new JLabel();
        writerHead = new JLabel();      writerLabel = new JLabel();
        ratingHead = new JLabel();      ratingLabel = new JLabel();
        scoreHead = new JLabel();       scoreLabel = new JLabel();
        metaHead = new JLabel();        metaLabel = new JLabel();
        pathHead = new JLabel();        pathLabel = new JLabel();
        sumHead = new JLabel();         sumLabel = new JTextArea(5,200);
        //---------
        sumLabel.setBackground(new Color(0,0,0));
        sumLabel.setOpaque(false);
        sumLabel.setWrapStyleWord(true);
        sumLabel.setLineWrap(true);
        sumLabel.setEditable(false);
        //---------
        movieBtn.setIcon(new ImageIcon(path + "noImage.png"));
        movieBtn.setContentAreaFilled(false);
        backBtn.setIcon(new ImageIcon(path + "exit.png"));
        backBtn.setContentAreaFilled(false);
        nameHead.setText("Movie:");         nameLabel.setText("Empty");
        yearHead.setText("Year:");          yearLabel.setText("Empty");
        countryHead.setText("Country:");    countryLabel.setText("Empty");
        langHead.setText("Language:");      langLabel.setText("Empty");
        duratHead.setText("Duration:");     duratLabel.setText("Empty");
        directHead.setText("Director:");    directLabel.setText("Empty");
        actorHead.setText("Actors:");       actorLabel.setText("Empty");
        writerHead.setText("Writers:");     writerLabel.setText("Empty");
        ratingHead.setText("Rating:");      ratingLabel.setText("Empty");
        scoreHead.setText("IMDb:");         scoreLabel.setText("Empty");
        metaHead.setText("Metascore:");     metaLabel.setText("Empty");
        pathHead.setText("Path:");          pathLabel.setText("Empty");
        sumHead.setText("Summary:");        sumLabel.setText("Empty");
        //---------
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(movieBtn, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(nameHead)
                            .addComponent(yearHead)
                            .addComponent(countryHead)
                            .addComponent(langHead)
                            .addComponent(duratHead)
                            .addComponent(directHead)
                            .addComponent(actorHead)
                            .addComponent(writerHead)
                            .addComponent(ratingHead)
                            .addComponent(scoreHead)
                            .addComponent(metaHead)
                            .addComponent(pathHead)
                            .addComponent(sumHead))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(nameLabel)
                            .addComponent(yearLabel)
                            .addComponent(countryLabel)
                            .addComponent(langLabel)
                            .addComponent(duratLabel)
                            .addComponent(directLabel)
                            .addComponent(actorLabel)
                            .addComponent(writerLabel)
                            .addComponent(ratingLabel)
                            .addComponent(scoreLabel)
                            .addComponent(metaLabel)
                            .addComponent(pathLabel)
                            .addComponent(sumLabel)))
                    .addComponent(backBtn, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(114, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(movieBtn, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(nameHead)
                            .addComponent(nameLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(yearHead)
                            .addComponent(yearLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(countryHead)
                            .addComponent(countryLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(langHead)
                            .addComponent(langLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(duratHead)
                            .addComponent(duratLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(directHead)
                            .addComponent(directLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(actorHead)
                            .addComponent(actorLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(writerHead)
                            .addComponent(writerLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(ratingHead)
                            .addComponent(ratingLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(scoreHead)
                            .addComponent(scoreLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(metaHead)
                            .addComponent(metaLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(pathHead)
                            .addComponent(pathLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(sumHead)
                            .addComponent(sumLabel))))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(backBtn, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }
    //-----------------------
    private void setPoster(ImageIcon img) {
        movieBtn.setIcon(img);
        movieBtn.addActionListener( new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                File f = movie.getFile();
                Desktop desktop;

                desktop = Desktop.getDesktop();
                try {
                    desktop.open(f);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                CardLayout cardLayout = (CardLayout) cards.getLayout();
                cardLayout.show(cards,"mainWindow");
            }
        });
    }
    private void setLabelData() {
        nameLabel.setText(movie.getName());
        yearLabel.setText(movie.getYear());
        countryLabel.setText(movie.getCountryStr());
        langLabel.setText(movie.getLanguageStr());
        duratLabel.setText(movie.getDuration());
        directLabel.setText(movie.getDirector());
        actorLabel.setText(movie.getActorsStr());
        writerLabel.setText(movie.getWrittersStr());
        ratingLabel.setText(movie.getIMDb_ContRat());
        scoreLabel.setText(movie.getIMDb_rating());
        metaLabel.setText(movie.getIMDb_metascore());
        pathLabel.setText(movie.getFile().getPath());
        sumLabel.setText(movie.getSummary());
    }
    //-----------------------
    public void addCards(JPanel pan) {
        cards = pan;
        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) cards.getLayout();
                cardLayout.show(cards, "mainWindow");
            }
        });
    }
    public void setMovie(Movie m) {
        movie = m;
        setLabelData();
        setPoster(movie.getPoster());
    }

}