package danielcortes.xyz.controllers;

import danielcortes.xyz.controllers.actions.BasicAction;
import danielcortes.xyz.data.Configuration;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

class BaseController {

  static void moveTo(JComponent origin, JComponent destiny) {
    KeyStroke next = KeyStroke.getKeyStroke("ENTER");
    KeyStroke back = KeyStroke.getKeyStroke("ESCAPE");
    origin.getInputMap(JComponent.WHEN_FOCUSED).put(next, "nextField");
    destiny.getInputMap(JComponent.WHEN_FOCUSED).put(back, "previousField");
    origin.getActionMap().put("nextField", (BasicAction) e -> destiny.requestFocus());
    destiny.getActionMap().put("previousField", (BasicAction) e -> origin.requestFocus());
  }

  static void doAction(JComponent target, String name, KeyStroke keyStroke, BasicAction action) {
    target.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, name);
    target.getActionMap().put(name, action);
  }

  static void launchFrame(JComponent view, String title) {
    JFrame frame = new JFrame(title + ": " + Configuration.get("nombre_caja"));
    frame.setContentPane(view);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  static void launchFrame(JComponent view, String title, Dimension d) {
    JFrame frame = new JFrame(title + ": " + Configuration.get("nombre_caja"));
    frame.setContentPane(view);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    frame.setSize(d);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
