/*
 * 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 com.greinet.tvtotalripper.ui;

import com.greinet.tvtotalripper.download.DownloadTask;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author agreiner
 */
public class DownloadTaskInformationPanel extends JPanel{
    
    /** The label for the title */
    private final JLabel labelTitle;
    /** The label for the show */
    private final JLabel labelShow;
    /** The label for the season */
    private final JLabel labelSeason;
    /** The label for the episode */
    private final JLabel labelEpisode;
    /** The label for the duration */
    private final JLabel labelDuration;
    /** The label for the download progress */
    private final JLabel labelProgress;
    /** The text field for the title */
    private final JTextField textTitle;
    /** The text field for the show */
    private final JTextField textShow;
    /** The text field for the season */
    private final JTextField textSeason;
    /** The text field for the episode */
    private final JTextField textEpisode;
    /** The text field for the duration */
    private final JTextField textDuration;
    /** The text field for the download progress */
    private final JProgressBar progressBar;
    
    /** The currently visualized download task */
    public DownloadTask selectedDownloadTask;
    
    /**
     * Create a Panel for DownloadTask information visualization
     */
    public DownloadTaskInformationPanel(){
        
        labelTitle = new JLabel("Title");
        labelShow = new JLabel("Show");
        labelSeason = new JLabel("Season");
        labelEpisode = new JLabel("Episode");
        labelDuration = new JLabel("Duration");
        labelProgress = new JLabel("Progress");
        
        textTitle = new JTextField();
        textTitle.setEditable(false);
        
        textShow = new JTextField();
        textShow.setEditable(false);
        
        textSeason = new JTextField();
        textSeason.setEditable(false);
        
        textEpisode = new JTextField();
        textEpisode.setEditable(false);
        
        textDuration = new JTextField();
        textDuration.setEditable(false);
        
        progressBar = new JProgressBar(0, 100);
        
        setLayout(new GridLayout(6, 2));
        
        add(labelTitle);
        add(textTitle);
        add(labelShow);
        add(textShow);
        add(labelSeason);
        add(textSeason);
        add(labelEpisode);
        add(textEpisode);
        add(labelDuration);
        add(textDuration);
        add(labelProgress);
        add(progressBar);
    }
    
    /**
     * Clear all information
     */
    public void clearInformation(){
        this.selectedDownloadTask = null;
        textTitle.setText("");
        textShow.setText("");
        textSeason.setText("");
        textEpisode.setText("");
        textDuration.setText("");
        progressBar.setValue(0);
        progressBar.setString("");
    }
    
    /**
     * Set the information of the download task
     * @param downloadTask the download task
     */
    public void setInformation(DownloadTask downloadTask){
        textTitle.setText(downloadTask.getEpisodeWrapper().getTitle());
        textTitle.setEditable(false);
        textShow.setText(downloadTask.getEpisodeWrapper().getShow());
        textShow.setEditable(false);
        textSeason.setText(downloadTask.getEpisodeWrapper().getSeason());
        textSeason.setEditable(false);
        textEpisode.setText(downloadTask.getEpisodeWrapper().getEpisode());
        textEpisode.setEditable(false);
        textDuration.setText(downloadTask.getEpisodeWrapper().getDuration());
        
        if(selectedDownloadTask != null && selectedDownloadTask.equals(downloadTask)){
            progressBar.setValue(downloadTask.getPercentCompleted());
            progressBar.setStringPainted(true);
            String done = FileUtils.byteCountToDisplaySize(downloadTask.getTotalBytesRead());
            String todo = FileUtils.byteCountToDisplaySize(downloadTask.getFileSize());
            progressBar.setString(done+"/"+todo);
        }
        
        downloadTask.addPropertyChangeListener((PropertyChangeEvent evt) -> {
            
            if(selectedDownloadTask != null && evt.getPropertyName().equals(Long.toString(selectedDownloadTask.getEpisodeWrapper().getId()))){
                progressBar.setValue((int)evt.getNewValue());
                progressBar.setStringPainted(true);
                String done = FileUtils.byteCountToDisplaySize(downloadTask.getTotalBytesRead());
                String todo = FileUtils.byteCountToDisplaySize(downloadTask.getFileSize());
                progressBar.setString(done+"/"+todo);
            }
        });
    }
    
    
    
}
