package com.example.cezenPBX.service;

import com.example.cezenPBX.DAO.CezenPbxOpsDAO;
import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.entity.ExtensionsTable;
import com.example.cezenPBX.entity.PsAors;
import com.example.cezenPBX.entity.PsAuths;
import com.example.cezenPBX.entity.PsEndPoints;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BasicPbxService implements BasicPbxServiceInterface {

    @Autowired
    private CezenPbxOpsDAO cezenPbxOpsDAO;

    // the service method will create an instance of PsEndPoints entity for persistence
    @Override
    public boolean createANewEndpointService(String id, String transport, String aors, String auth,
                                             String context, String disallow, String allow, String directMedia,
                                             String connectedLineMethod, String callerid, String dtmfMode,
                                             String mohsuggest, String mailboxes) {

        // create the persistence object
        PsEndPoints psEndPoints = new PsEndPoints(
                id,
                transport,
                aors,
                auth,
                context,
                disallow,
                allow,
                directMedia,
                connectedLineMethod,
                callerid,
                dtmfMode,
                mohsuggest,
                mailboxes
        );

        // if there is an exception in the entity or transaction
        try{
            return cezenPbxOpsDAO.addNewEndPoint(psEndPoints);
        }catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public ReturnStatus addExtensionForUser(String context, String extension, int priority, String app, String appdata) {

        System.out.println("Context in SERVICE is " + context);

        try{
            // create a new entity of extension
             return this.cezenPbxOpsDAO.saveAnExtension(new ExtensionsTable(
                    context,
                    extension,
                    priority,
                    app,
                    appdata
            ));
        }catch ( Exception e){
            // the persisting data already exists or it's a db issue
            return new ReturnStatus(false, "Data likely already exists or DB issue",e.getMessage());
        }
    }

    @Override
    public ReturnStatus addAPasswordForEndpoint(String id, String authType, String userName, String password, String md5Cred, String realm) {

        // create the new endpoint password instance
        PsAuths psAuths = new PsAuths(id, authType, userName, password, md5Cred, realm);

        // send it to the DAO
        try{
            return this.cezenPbxOpsDAO.setThePasswordForSipEndpoint(psAuths);

        }catch(Exception e){
            return new ReturnStatus(false, "Endpoint and password already set ",e.getMessage());
        }
    }

    @Override
    public ReturnStatus setAors(String id, int maxContacts) {

        PsAors psAors = new PsAors(id, maxContacts);

        try{
            return this.cezenPbxOpsDAO.setTheAorsForTheEndPoint(psAors);
        }catch (Exception e){
            return new ReturnStatus(false, "Endpoint and password already set ",e.getMessage());
        }
    }

    @Override
    public boolean deleteByExtension(String extensionNumber) {

        return this.cezenPbxOpsDAO.deleteAnExtensionPoint(extensionNumber);
    }

    // TODO make sure you check if the extension field is a literal character
    @Override
    public ReturnStatus addFeaturesToExtensionsForEndpoints(String context, String extension, int priority, String app, String appdata) {
        // check if extension is a character
        String extensionWordLiteral = "";

        if(extension.chars().allMatch(Character::isLetter)){

            extensionWordLiteral = extension;
        }else{

            return new ReturnStatus(false, "Only Character type extension point allowed", "User entered a numerical character sequence expected an alphabetical one instead");
        }


        //if received input is empty
        if(extensionWordLiteral.isEmpty()) return new ReturnStatus(false, "Extension field is empty", "User entered an empty extension");

        // if yes then add the feature to the database
        //return the status of the feature addition
        return this.cezenPbxOpsDAO.saveAnExtensionByCharacters(
                new ExtensionsTable(
                        context,
                        extension,
                        priority,
                        app,
                        appdata
                )
        );
    }
}
