/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jfc.eclinic.services.impl;

import com.jfc.eclinic.dao.AdminClinicRepository;
import com.jfc.eclinic.dao.exception.RepositoryException;
import com.jfc.eclinic.dto.Adminclinic;
import com.jfc.eclinic.dto.form.AdminClinicForm;
import com.jfc.eclinic.services.AdminClinicService;
import com.jfc.eclinic.services.exception.PasswordException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.xml.bind.DatatypeConverter;

/**
 *
 * @author jfc
 */
@Stateless
public class AdminClinicServiceImpl implements AdminClinicService {

    private AdminClinicRepository adminClinicRepository;

    @Inject
    public AdminClinicServiceImpl(AdminClinicRepository adminClinicRepository) {
        this.adminClinicRepository = adminClinicRepository;
    }

    public AdminClinicServiceImpl() {
    }

    @Override
    public List<Adminclinic> get() throws Exception {
        return adminClinicRepository.findAll();
    }

    @Override
    public Adminclinic get(int id) throws Exception {
        return adminClinicRepository.findById(id);
    }

    @Override
    public void update(Adminclinic value) throws Exception {
        adminClinicRepository.update(value);
    }

    @Override
    public void create(Adminclinic value) throws Exception {
        adminClinicRepository.add(value);
    }

    @Override
    public void delete(Adminclinic id) throws Exception {
        adminClinicRepository.remove(id);
    }

    @Override
    public void updateName(AdminClinicForm value) throws Exception {
        Adminclinic adminclinic = adminClinicRepository.findById(Integer.valueOf(value.getId()));
        adminclinic.getPersonId().setFirstName(value.getFirstName());
        adminclinic.getPersonId().setLastName(value.getLastName());
        adminClinicRepository.update(adminclinic);
    }

    @Override
    public void updatePassword(AdminClinicForm value) throws PasswordException, RepositoryException, NoSuchAlgorithmException {
        if (value.getCurrentPassword() == null || value.getConfirmPassword() == null || value.getNewPassword() == null) {
            throw new PasswordException("Please fill all the required filds");
        }
        if (!value.getConfirmPassword().equals(value.getNewPassword())) {
            throw new PasswordException("Password didnt match");
        }

        Adminclinic adminclinic = adminClinicRepository.findById(Integer.valueOf(value.getId()));
        if (!encodehashPassword(value.getCurrentPassword()).equals(adminclinic.getPassCode())) {
            throw new PasswordException("Old password didnt match");
        }

        adminclinic.setPassCode(encodehashPassword(value.getNewPassword()));
        adminClinicRepository.update(adminclinic);
    }

    private String encodehashPassword(String password) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");// SHA-512m SHA-256
        byte[] message = password.getBytes();
        md.update(message);
        byte[] digestbyte = md.digest();
        password = DatatypeConverter.printHexBinary(digestbyte).toLowerCase();

        return password;
    }

}