package org.bucksch.zeipis.recorder;
import org.bucksch.zeipis.all.*;
import java.sql.*;

abstract public class Recording
{
  public Recording(int id)
  {
    myID = id;
  }
  protected void readDatabase(String select)
    throws Misconfiguration, SQLException
  {
    ResultSet rs = DB.me.database().createStatement().executeQuery(select);
    if (!rs.first())
      throw new Misconfiguration("No such recording ID " + myID);
    try {
      readDatabaseResult(rs);
    } catch (Exception e) {
      throw new Misconfiguration("Recording ID " + myID + " misconfigured in database", e);
    }
  }
  // separated for inheriting classes
  protected void readDatabaseResult(ResultSet rs)
    throws SQLException, Misconfiguration
  {
    myTitle = rs.getString("title");
    mySubtitle = rs.getString("subtitle");
    myOriginalTitle = rs.getString("original_title");
    myOriginalSubtitle = rs.getString("original_subtitle");
    myDescription = rs.getString("description");
    myGenre = rs.getString("genre");
    myShowType = rs.getString("showtype");
    myFirstShown = rs.getInt("firstshown");

    // check
    if (myTitle.length() == 0)
      throw new IllegalStateException("No program/title found for recording ID " + myID);
  }

  // Getters
  public int id() { return myID; };
  public String title() { return myTitle; };
  public String subtitle() { return mySubtitle; };
  public String originalTitle() { return myOriginalTitle; };
  public String originalSubtitle() { return myOriginalSubtitle; };
  public String description() { return myDescription; };
  public String genre() { return myGenre; };
  public String showType() { return myShowType; };
  public int firstShownInYear() { return myFirstShown; };
  public String toString()
  {
    return "recordID " + myID + " " + myTitle + " - " + mySubtitle;
  }

  // Data
  protected int myID;
  protected String myTitle;
  protected String mySubtitle;
  protected String myOriginalTitle;
  protected String myOriginalSubtitle;
  protected String myDescription;
  protected String myGenre;
  protected String myShowType; // "movie", "series", "sports" etc.
  protected int myFirstShown; // year
}
