package zustand;

public class StateCard extends AbstractState{

private Machine mach;
private int money;
private Card my_card;


  public StateCard(Machine ma, Card c) {
    mach = ma;
    my_card = c;
    money = 0;
  }

  private void eject() {
    StateEmpty new_state = new StateEmpty(mach);
    System.out.println("Ejecting Card (value = " + my_card.get_money() + ").");
    mach.set_state(new_state);
  }

  private void write_card(int m) {
    System.out.println("Writing Card (new value is " + m + ")." );
    my_card.set_money(m);
  }

  private void keep_money() {
    System.out.println("Keeping Money: " + money);
    money =0;
  }

  private void accept_money(int m) {
    if(m <= (my_card.max_money - (my_card.get_money() + money))) {
      money = money + m;
    }
  }

  private void reject_money(int m) {
     System.out.println("Too much Money!!! REJECTING " + m + "!!!" );
  }

  private void give_back_money() {
    System.out.println("Giving back Money: " + money);
    money =0;
  }

  private void do_abort() {
    this.give_back_money();
    this.eject();
  }

  public void show_state() {
    System.out.println("state: card! \n money on Card: " + my_card.get_money() + "\n max money on Card: " + my_card.max_money + "\n money in Machine: " + money);
  }

  public void abort() {
    do_abort();
  }

  public void finish() {
    write_card (my_card.get_money() + money);
    keep_money();
    eject();
  }

  public boolean insert_card(Card c) {
    System.out.println("ERROR: New card while card is already in machine! \n ==> PANIK ABORT!!!");
    do_abort();
    return false;
  }

  public void add_money(int m) {
    if (m > my_card.max_money - (money + my_card.get_money())) {
      reject_money (m);
    } else { accept_money (m); }
  }

  public Card get_card() {
    return my_card;
  }

  public int get_money() {
    return money;
  }
  
}