package com.docus.server;

import com.docus.server.message.consts.HospitalWsMethod;
import com.docus.server.message.service.FsSyWebServiceUnifyMessageService;
import com.docus.server.message.service.ReportFileMessageService;
import com.docus.server.message.service.SdWebServiceUnifyMessageService;
import com.docus.server.message.service.UnifyMessageService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    private UnifyMessageService unifyMessageService;

    private SdWebServiceUnifyMessageService sdWebServiceUnifyMessageService;

    private FsSyWebServiceUnifyMessageService fsSyWebServiceUnifyMessageService;

    private ReportFileMessageService reportFileMessageService;

    @Autowired
    public void setFsSyWebServiceUnifyMessageService(FsSyWebServiceUnifyMessageService fsSyWebServiceUnifyMessageService) {
        this.fsSyWebServiceUnifyMessageService = fsSyWebServiceUnifyMessageService;
    }

    @Autowired
    public void setUnifyMessageService(UnifyMessageService unifyMessageService) {
        this.unifyMessageService = unifyMessageService;
    }

    @Autowired
    @Qualifier(HospitalWsMethod.WS_RECORD_SUBMIT)
    public void setSdWebServiceUnifyMessageService(SdWebServiceUnifyMessageService sdWebServiceUnifyMessageService) {
        this.sdWebServiceUnifyMessageService = sdWebServiceUnifyMessageService;
    }

    @Autowired
    public void setReportFileMessageService(ReportFileMessageService reportFileMessageService) {
        this.reportFileMessageService = reportFileMessageService;
    }


    /**
     * 注入Servlet，注意beanName不能为dispatcherServlet
     *
     * @author Fang Ruichuan
     * @date 2022/11/14 19:16
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    @Qualifier("unifyEndPoint")
    public Endpoint unifyEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), unifyMessageService);
        endpoint.publish("/unify");
        return endpoint;
    }

    @Bean
    @Qualifier("fssyEndPoint")
    public Endpoint fssyEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), fsSyWebServiceUnifyMessageService);
        endpoint.publish("/fssy");
        return endpoint;
    }


    @Bean
    @Qualifier("wsRecordSubmitEndPoint")
    public Endpoint wsRecordSubmitEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), sdWebServiceUnifyMessageService);
        endpoint.publish("/WS_RECORD_SUBMIT");
        return endpoint;
    }

    @Bean
    @Qualifier("reportFileEndPoint")
    public Endpoint reportFileEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), reportFileMessageService);
        endpoint.publish("/reportFile");
        return endpoint;
    }
}
