package com.ywt.controller;

import com.ywt.gapi.taihe.biz.GetScreeningTableRequest;
import com.ywt.gapi.taihe.biz.GetScreeningTableResponse;
import com.ywt.gapi.taihe.biz.TaiheBizService;
import com.ywt.outpatient.api.mri.Question;
import com.ywt.outpatient.api.mri.QuestionOption;
import com.ywt.outpatient.api.mri.ScreeningTable;
import com.ywt.outpatient.api.resp.BaseResponse2;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class Consumer {

    @DubboReference
    private TaiheBizService taiheBizServiceBlockingStub;


    @GetMapping("/tri")
    public void tri() {
        try {
            System.out.println(1111);
            GetScreeningTableRequest getScreeningTableRequest = GetScreeningTableRequest.newBuilder()
                    .setUserId(13140)
                    .build();
            GetScreeningTableResponse response = taiheBizServiceBlockingStub.getScreeningTable(getScreeningTableRequest);
            if (response.getCode() == BaseResponse2.SUCCEED) {
                ScreeningTable resp = new ScreeningTable();
                com.ywt.gapi.taihe.biz.ScreeningTable table = response.getScreeningTable();
                if (table != null) {
                    com.ywt.gapi.taihe.biz.PatientInfo info = table.getPatientInfo();
                    com.ywt.outpatient.api.mri.PatientInfo patientInfo = new com.ywt.outpatient.api.mri.PatientInfo();
                    if (info != null) {
                        patientInfo.setPatientName(info.getPatientName());
                        patientInfo.setSex(info.getSex());
                        patientInfo.setBirthday(info.getBirthday());
                        patientInfo.setPatientMobile(info.getPatientMobile());
                        patientInfo.setAddress(info.getAddress());
                    }
                    List<Question> questions = table.getQuestionsList().stream()
                            .map(question -> {
                                com.ywt.outpatient.api.mri.Question q = new com.ywt.outpatient.api.mri.Question();
                                q.setQuestionId(question.getQuestionId());
                                q.setCategory(question.getCategory());
                                q.setOptions(convertGrpcDefObj2Model(question.getOptionsList()));
                                return q;
                            })
                            .collect(Collectors.toList());
                    resp.setPatientInfo(patientInfo);
                    resp.setQuestions(questions);
                    resp.setTableId(table.getTableId());
                }
                System.out.println("Receive resp ======> " + resp);
            }
            System.out.println("Receive result ======> " + response.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<QuestionOption> convertGrpcDefObj2Model(List<com.ywt.gapi.taihe.biz.QuestionOption> list) {
        return list.stream()
                .map(questionOption -> {
                    QuestionOption option = new QuestionOption();
                    List<com.ywt.gapi.taihe.biz.QuestionOption> subList = questionOption.getChildrenList();
                    List<QuestionOption> children = new LinkedList<>();
                    if (subList != null && !subList.isEmpty()) {
                        children = convertGrpcDefObj2Model(subList);
                    }
                    option.setId(questionOption.getId());
                    option.setName(questionOption.getName());
                    option.setValue1(questionOption.getValue1());
                    option.setValue2(questionOption.getValue2());
                    option.setValue3(questionOption.getValue3());
                    option.setChildren(children);
                    return option;
                })
                .collect(Collectors.toList());
    }
}
