/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.qvm.service;

import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.repo.SequenceRepository;
import com.axelor.apps.base.service.administration.SequenceService;
import com.axelor.apps.qvm.db.QvmOperation;
import com.axelor.apps.qvm.db.repo.QvmOperationRepository;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.repo.TraceBackRepository;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class QvmOperationServiceImpl implements QvmOperationService {

  @Inject protected SequenceService sequenceService;

  @Transactional
  public QvmOperation createCalibrationSeq(Company company) throws AxelorException {

    QvmOperation calibration = new QvmOperation();
    calibration.setSerialNumber(this.getCalibrationSequence(company));

    return calibration;
  }

  @Transactional
  public QvmOperation createQualificationSeq(Company company) throws AxelorException {

    QvmOperation qualification = new QvmOperation();
    qualification.setSerialNumber(this.getQualificationSequence(company));

    return qualification;
  }

  @Transactional
  public QvmOperation createOperationSeq(Company company) throws AxelorException {

    QvmOperation operation = new QvmOperation();
    operation.setSerialNumber(this.getOperationSequence(company));

    return operation;
  }

  public String getCalibrationSequence(Company company) throws AxelorException {
    String seq = sequenceService.getSequenceNumber(SequenceRepository.CALIB_SEQ, company);
    if (seq == null) {
      throw new AxelorException(
          company,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get("Calibration Sequence is not defined"),
          company.getName());
    }
    return seq;
  }

  public String getQualificationSequence(Company company) throws AxelorException {
    String seq = sequenceService.getSequenceNumber(SequenceRepository.QUAL_SEQ, company);
    if (seq == null) {
      throw new AxelorException(
          company,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get("Qualification Sequence is not defined"),
          company.getName());
    }
    return seq;
  }

  public String getOperationSequence(Company company) throws AxelorException {
    String seq = sequenceService.getSequenceNumber(SequenceRepository.OP_SEQ, company);
    if (seq == null) {
      throw new AxelorException(
          company,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get("Operation Sequence is not defined"),
          company.getName());
    }
    return seq;
  }

  public QvmOperation setMachine(QvmOperation calibration) throws AxelorException {
    if (calibration.getMachineName() != null) {
      System.out.println("calibration.getMachineName : " + calibration.getMachineName());
    }
    return calibration;
  }

  public QvmOperation createNextOperation(QvmOperation operation, Long userId) {
    QvmOperation nextOperation = Beans.get(QvmOperationRepository.class).copy(operation, true);
    if (operation != null) {
      try {
        nextOperation.setStatusSelect(1);
        nextOperation.setOperationOrigin(operation.getSerialNumber());
        nextOperation.setCreationType(2);
        nextOperation.setPastOperationDate(operation.getOperationDate());
        nextOperation.setOperationDate(operation.getOperationDueDate());
        if (operation.getOperationFrequency() == 1) {
          nextOperation.setOperationDueDate(operation.getOperationDueDate().plusYears(1));
          nextOperation.setCountdown(364);
        }
        if (operation.getOperationFrequency() == 2) {
          nextOperation.setOperationDueDate(operation.getOperationDueDate().plusMonths(1));
          nextOperation.setCountdown(31);
        }
        if (operation.getOperationFrequency() == 3) {
          nextOperation.setOperationDueDate(operation.getOperationDueDate().plusMonths(6));
          nextOperation.setCountdown(183);
        }
        System.out.println(
            "operation.getOperationFrequency : " + operation.getOperationFrequency());
        nextOperation.setSerialNumber(null);
        nextOperation.setCreationType(2);
      } catch (Exception exception) {
        System.out.println(exception);
      }
    }
    // Beans.get(QvmOperationRepository.class).save(nextOperation);
    return nextOperation;
  }
}
