package xyz.wbsite.action.ajax.conf;

import org.springframework.beans.factory.annotation.Autowired;
import xyz.wbsite.frame.auth.LocalData;
import xyz.wbsite.module.conf.mgr.MappingManager;
import xyz.wbsite.module.conf.req.MappingCreateRequest;
import xyz.wbsite.module.conf.req.MappingDeleteRequest;
import xyz.wbsite.module.conf.req.MappingFindRequest;
import xyz.wbsite.module.conf.req.MappingGetRequest;
import xyz.wbsite.module.conf.req.MappingUpdateRequest;
import xyz.wbsite.module.conf.req.NginxReloadRequest;
import xyz.wbsite.module.conf.rsp.MappingCreateResponse;
import xyz.wbsite.module.conf.rsp.MappingDeleteResponse;
import xyz.wbsite.module.conf.rsp.MappingFindResponse;
import xyz.wbsite.module.conf.rsp.MappingGetResponse;
import xyz.wbsite.module.conf.rsp.MappingUpdateResponse;

public class MappingAjax {

    @Autowired
    private MappingManager mappingManager;

    public MappingCreateResponse create(MappingCreateRequest request) {
        MappingCreateResponse response = mappingManager.create(request, LocalData.getToken());
        if (!response.hasError()) {
            NginxAjax nginxAjax = LocalData.getBean(NginxAjax.class);
            nginxAjax.flushConfig();
            nginxAjax.reload(new NginxReloadRequest());
        }
        return response;
    }

    public MappingDeleteResponse delete(MappingDeleteRequest request) {
        MappingDeleteResponse response = mappingManager.delete(request, LocalData.getToken());
        if (!response.hasError()) {
            NginxAjax nginxAjax = LocalData.getBean(NginxAjax.class);
            nginxAjax.flushConfig();
            nginxAjax.reload(new NginxReloadRequest());
        }
        return response;
    }

    public MappingUpdateResponse update(MappingUpdateRequest request) {
        MappingUpdateResponse response = mappingManager.update(request, LocalData.getToken());
        if (!response.hasError()) {
            NginxAjax nginxAjax = LocalData.getBean(NginxAjax.class);
            nginxAjax.flushConfig();
            nginxAjax.reload(new NginxReloadRequest());
        }
        return response;
    }

    public MappingFindResponse find(MappingFindRequest request) {
        return mappingManager.find(request, LocalData.getToken());
    }

    public MappingGetResponse get(MappingGetRequest request) {
        return mappingManager.get(request, LocalData.getToken());
    }
}
