
package de.ivu.elect.business.konfiguration.entity;

import de.ivu.elect.business.konfiguration.control.BigDecimalValidator;
import de.ivu.elect.business.konfiguration.control.VerzeichnisKonfiguriertValidator;
import de.ivu.elect.business.konfiguration.control.VerzeichnisNichtUnterhalbAblageVerzeichnisValidator;
import de.ivu.elect.jee.faces.CronExpressionValidator;
import de.ivu.elect.jee.faces.EnumValidator;
import de.ivu.elect.jee.faces.VerzeichnisValidator;
import de.ivu.elect.jee.presentation.Icons;
import de.ivu.elect.util.VerzeichnisPruefer;

import javax.faces.validator.LongRangeValidator;
import javax.faces.validator.RequiredValidator;
import javax.faces.validator.Validator;
import java.util.ArrayList;
import java.util.List;

/**
 *  on 13.07.2015.
 */
public enum CustomParameterWertTyp {

  VOID {

    @Override
    public boolean expectsWertIsReadonly() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypVoid();
    }
  },
  STRING {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypString();
    }
  },
  STRING_MULTILINE {

    @Override
    public String getIcon(Icons icons) {
      return STRING.getIcon(icons);
    }
  },
  STRING_SECRET {

    @Override
    public boolean expectsWertIsSecret() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconKey();
    }
  },
  DATE {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconKalender();
    }

    @Override
    public boolean expectsWertIsDate() {
      return true;
    }
  },
  TIMESTAMP {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypTimestamp();
    }

    @Override
    public boolean expectsWertIsTimestamp() {
      return true;
    }
  },
  CRONTAB {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypCron();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new CronExpressionValidator());
      return validators;
    }

    @Override
    public boolean expectsWertIsCrontab() {
      return true;
    }
  },
  BOOLEAN {

    @Override
    public boolean expectsWertIsBoolean() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypBoolean();
    }
  },
  LONG {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypLong();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new LongRangeValidator(Long.MAX_VALUE, Long.MIN_VALUE));
      return validators;
    }
  },
  @Deprecated // Warum nicht einfach Long nehmen?
  INTEGER {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypInteger();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new LongRangeValidator(Integer.MAX_VALUE, Integer.MIN_VALUE));
      return validators;
    }

  },
  BIGDECIMAL {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypInteger();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new BigDecimalValidator());
      return validators;
    }

  },
  VERZEICHNIS {

    @Override
    public boolean expectsWertIsVerzeichnis() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypVerzeichnis();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new VerzeichnisValidator(VerzeichnisPruefer::testeVerzeichnisExistiert));
      validators.add(new VerzeichnisNichtUnterhalbAblageVerzeichnisValidator());
      return validators;
    }
  },
  VERZEICHNIS_BESCHREIBBAR {

    @Override
    public boolean expectsWertIsVerzeichnis() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypVerzeichnisBeschreibbar();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new VerzeichnisValidator(VerzeichnisPruefer::testeVerzeichnisExistiertUndIstBeschreibbar));
      validators.add(new VerzeichnisNichtUnterhalbAblageVerzeichnisValidator());

      return validators;
    }

  },
  // ELECTMUC-2206 Ohne Unterhalb von Ablageverzeichnisvalidator
  VERZEICHNIS_BESCHREIBBAR_ABLAGE_VERZEICHNIS {

    @Override
    public boolean expectsWertIsVerzeichnis() {
      return true;
    }

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypVerzeichnisBeschreibbar();
    }

    @Override
    public List<Validator> getValidators(CustomParameter< ? > parameter) {
      List<Validator> validators = super.getValidators(parameter);
      validators.add(new VerzeichnisKonfiguriertValidator());
      validators.add(new VerzeichnisValidator(VerzeichnisPruefer::testeVerzeichnisExistiertUndIstBeschreibbar));

      return validators;
    }
  },
  ENUM {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypEnum();
    }

    @Override
    public List<Validator> getValidators(CustomParameter<?> parameter) {
      List<Validator> validators = super.getValidators(parameter);
      // ELECTMUC-2729
      validators.add(new RequiredValidator());
      parameter.getWertEnumClass().ifPresent(aClass -> validators.add(new EnumValidator(aClass)));
      return validators;
    }
  },
  SELECT {

    @Override
    public String getIcon(Icons icons) {
      return icons.getIconTypSelect();
    }

    @Override
    public boolean expectsWertIsSelect() {
      return true;
    }
  };

  private CustomParameterWertTyp() {
  }

  public boolean expectsWertIsBoolean() {
    return false;
  }

  public boolean expectsWertIsDate() {
    return false;
  }

  public boolean expectsWertIsTimestamp() {
    return false;
  }

  public boolean expectsWertIsReadonly() {
    return false;
  }

  public boolean expectsWertIsVerzeichnis() {
    return false;
  }

  public boolean expectsWertIsSelect() {
    return false;
  }

  public boolean expectsWertIsCrontab() {
    return false;
  }

  public boolean expectsWertIsSecret() {
    return false;
  }

  public abstract String getIcon(Icons icons);

  public List<Validator> getValidators(CustomParameter< ? > parameter) {
    return new ArrayList<>();
  }
}
