package com.eactive.eai.rms.onl.manage.authoutbound;

import com.eactive.eai.common.seed.Seed;
import com.eactive.eai.data.entity.onl.authoutbound.OutboundOAuthCredential;
import com.eactive.eai.data.entity.onl.property.PropGroup;
import com.eactive.eai.rms.data.entity.onl.authoutbound.OutboundOAuthCredentialService;
import com.eactive.eai.rms.onl.manage.adapter.adapter.AdapterManService;
import com.eactive.eai.rms.onl.manage.adapter.adapter.ui.AdapterGroupUI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class OutboundOAuthCredentialManService {

    public static final Log logger = LogFactory.getLog(OutboundOAuthCredentialManService.class);

    @Autowired
    OutboundOAuthCredentialService service;

    @Autowired
    OutboundOAuthCredentialUIMapper mapper;

    @Autowired
    private AdapterManService adapterService;

    public Page<OutboundOAuthCredentialUI> selectList(Pageable pageable, String searchAdapterGroupName) {
        Page<OutboundOAuthCredential> page = service.findAll(pageable, searchAdapterGroupName);
        return page.map(mapper::toVo);
    }

    public OutboundOAuthCredentialUI selectDetail(String searchAdapterGroupName) throws Exception {
        OutboundOAuthCredential outboundOAuthCredential = service.getById(searchAdapterGroupName);
        if(outboundOAuthCredential == null){
            return null;
        }
        OutboundOAuthCredentialUI ui = mapper.toVo(outboundOAuthCredential);
        decryptClientSecret(ui);
        return ui;
    }

    public void insert(OutboundOAuthCredentialUI outboundOAuthCredentialUI) throws Exception {
        encryptClientSecret(outboundOAuthCredentialUI);
        OutboundOAuthCredential entity = mapper.toEntity(outboundOAuthCredentialUI);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreatedAt(now);
        entity.setUpdatedAt(now);
        service.save(entity);
    }

    public void update(OutboundOAuthCredentialUI outboundOAuthCredentialUI) throws Exception {
        OutboundOAuthCredential entity = service.getById(outboundOAuthCredentialUI.getAdapterGroupName());
        outboundOAuthCredentialUI.setCreatedAt(entity.getCreatedAt());
        encryptClientSecret(outboundOAuthCredentialUI);
        mapper.updateToEntity(outboundOAuthCredentialUI, entity);
        entity.setUpdatedAt(LocalDateTime.now());
        service.save(entity);
    }

    public void delete(String adapterGroupName) {
        OutboundOAuthCredential entity = service.getById(adapterGroupName);
        if (entity != null) {
            service.deleteById(adapterGroupName);
        }
    }

    public HashMap selectDetailCombo() throws Exception {
        List<AdapterGroupUI> listTo = adapterService.selectListComboForAdptrIoDstcd("OU");
        HashMap map = new HashMap();
        map.put("toList", listTo);
        return map;
    }

    public void decryptClientSecret(OutboundOAuthCredentialUI ui) throws Exception {
        String clientSecret = ui.getClientSecret();
        String decrypted = Seed.decrypt(clientSecret);
        ui.setClientSecret(decrypted);
    }

    public void encryptClientSecret(OutboundOAuthCredentialUI ui) throws Exception {
        String clientSecret = ui.getClientSecret();
        String encrypted = Seed.encrypt(clientSecret);
        ui.setClientSecret(encrypted);
    }
}
