package com.ywt.outpatient.service.rpc;

import com.ywt.biz.common.enums.OrderTypeEnum;
import com.ywt.biz.common.util.serializers.JsonSerializer;
import com.ywt.gapi.im.IMService;
import com.ywt.gapi.im.NoticeMessage;
import com.ywt.gapi.im.SendResponse;
import com.ywt.outpatient.service.rpc.common.ServiceResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class PushServiceProvider {

    @DubboReference
    private IMService imServiceBlockingStub;

    /**
     * 转入医生收到转出医生的订单时，转入医生收到短信；
     * 专家收到基层医生的订单时，专家收到短信；
     * 专家收到基层医生的订单时，专家收到短信；
     * <p>
     * 模版名称:
     * 业务申请-通知
     * 模版CODE:
     * SMS_109470207
     * 模版内容:
     * ${name}向您发起${biz}申请！
     *
     * @param doctorMobile 接收医生手机号码
     * @param doctorName   发起医生的姓名
     * @param type         订单类型
     * @return
     */
    public ServiceResult notifyAfterCreateOrder(String doctorName, int type, int userId) {
        OrderTypeEnum orderTypeEnum = OrderTypeEnum.valueOf(type);
        String title = orderTypeEnum.getDisplayName();
        String content = String.format("%s向您发起%s申请！", doctorName, orderTypeEnum.getDisplayName());
        NotificationExtras notificationExtras = new NotificationExtras(1, "");

        String extras = JsonSerializer.toJson(notificationExtras);
        return notify(userId, title, content, extras);
    }


    public ServiceResult notifyAfterUserPaidConsultOrder(String patientName, int userId) {
        String content = String.format("%s患者向您发起了咨询！", patientName);
        return notify(userId, "问诊", content);
    }

    /**
     * 患者确认转诊后，转入、转出医生收到短信
     * <p>
     * 模版名称:
     * 双向转诊-患者确认转诊-通知医生
     * 模版CODE:
     * SMS_109520205
     * 模版内容:
     * ${patient}患者确认转入医院：${hospital}，科室：${dept}，床位：${num}，转入时间：${date}。
     *
     * @param patientName
     * @param hospital
     * @param dept
     * @param bedNumber
     * @param transferDate
     * @param doctorMobiles
     * @return
     */
    public ServiceResult notifyAfterUserCheckOrder(String patientName, String hospital, String dept, String bedNumber, String transferDate, int fromDoctorId, int toDoctorId) {
        ServiceResult serviceResult = new ServiceResult();
        String title = "患者确认转诊";
        String content = String.format("%s患者确认转入医院：%s，科室：%s，床位：%s，转入时间：%s。",
                patientName, hospital, dept, bedNumber, transferDate);
        NotificationExtras notificationExtras = new NotificationExtras(1, "");
        String extras = JsonSerializer.toJson(notificationExtras);
        notify(fromDoctorId, title, content, extras);
        notify(toDoctorId, title, content, extras);

        return serviceResult.succeed("处理成功");
    }


    /**
     * 患者支付后，基层医生收到短信
     * <p>
     * 模版CODE:
     * SMS_109480185
     * 模版内容:
     * ${patient}患者已经支付，请您在${date}协助诊疗。
     *
     * @param patientName
     * @param attendingDate
     * @param fromDoctorMobile
     * @return
     */
    public ServiceResult notifySponsorAfterUserPaid(String patientName, String attendingDate, int fromDoctorId) {
        String title = "患者已支付";
        String content = String.format("%s患者已经支付，请您在%s协助诊疗。", patientName, attendingDate);
        NotificationExtras notificationExtras = new NotificationExtras(1, "");

        String extras = JsonSerializer.toJson(notificationExtras);
        return notify(fromDoctorId, title, content, extras);
    }

    /**
     * 患者支付后，专家收到短信
     * <p>
     * 模版名称:
     * 名医主诊-通知专家
     * 模版CODE:
     * SMS_109560189
     * 模版内容:
     * ${patient}患者已经支付，请您在${date}到${hospital}为患者诊疗。
     *
     * @param patientName
     * @param attendingDate
     * @param hospital
     * @param toDoctorMobile
     * @return
     */
    public ServiceResult notifySpecialistAfterUserPaid(String patientName, String attendingDate, String hospital, int toDoctorId) {
        String title = "患者已支付";
        String content = String.format("%s患者已经支付，请您在%s到%s为患者诊疗。", patientName, attendingDate, hospital);
        NotificationExtras notificationExtras = new NotificationExtras(1, "");

        String extras = JsonSerializer.toJson(notificationExtras);
        return notify(toDoctorId, title, content, extras);
    }

    private ServiceResult notify(int userId, String title, String content) {
        NotificationExtras notificationExtras = new NotificationExtras(1, "");
        String extras = JsonSerializer.toJson(notificationExtras);
        return notify(userId, title, content, extras);
    }

    private ServiceResult notify(int userId, String title, String content, String extras) {
        ServiceResult serviceResult = new ServiceResult();
        NoticeMessage noticeMessage = NoticeMessage.newBuilder()
                .setToUserId(userId)
                .setTitle(title)
                .setContent(content)
                .setCustom(extras)
                .setTime(System.currentTimeMillis())
                .setType(3)
                .build();

        SendResponse sendResponse = imServiceBlockingStub.sendNoticeMessage(noticeMessage);

        if (sendResponse.getCode() == 0) {
            return serviceResult.succeed("处理成功");
        }

        return serviceResult.failed("处理失败");
    }

    private class NotificationExtras {
        private int msgType = 1;

        private String schema;

        public int getMsgType() {
            return msgType;
        }

        public NotificationExtras() {
        }

        public NotificationExtras(int msgType, String schema) {
            this.msgType = msgType;
            this.schema = schema;
        }

        public void setMsgType(int msgType) {
            this.msgType = msgType;
        }

        public String getSchema() {
            return schema;
        }

        public void setSchema(String schema) {
            this.schema = schema;
        }
    }
}
