package com.fs.app.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.core.domain.R;
import com.fs.course.enums.MiniProgramAgreementEnum;
import com.fs.his.config.AgreementConfig;
import com.fs.his.domain.MiniProgramAgreement;
import com.fs.his.service.MiniProgramAgreementService;
import com.fs.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Controller
@RequestMapping(value="/web")
public class H5Controller
{

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private MiniProgramAgreementService miniProgramAgreementService;


    @GetMapping("/userAgreement")
    public ModelAndView userAgreement( )
    {

        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);

        ModelAndView mv=new ModelAndView();
        mv.addObject("userAgreement", config.getUserRegister());
        mv.setViewName("userAgreement");
        return mv;
    }
    @GetMapping("/privacyPolicy")
    public ModelAndView privacyPolicy( )
    {
        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);
        ModelAndView mv=new ModelAndView();
        mv.addObject("privacyPolicy", config.getUserPrivacy());
        mv.setViewName("privacyPolicy");
        return mv;
    }

    @GetMapping("/healthCustomerService")
    public ModelAndView healthCustomerService( )
    {
        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);
        ModelAndView mv=new ModelAndView();
        mv.addObject("healthCustomerService", config.getUserHealth());
        mv.setViewName("healthCustomerService");
        return mv;
    }

    @GetMapping("/vipService")
    public ModelAndView vipService( )
    {
        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);
        ModelAndView mv=new ModelAndView();
        mv.addObject("vipService", config.getVipService());
        mv.setViewName("vipService");
        return mv;
    }


    @GetMapping("/vipAutomaticService")
    public ModelAndView vipAutomaticService( )
    {
        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);
        ModelAndView mv=new ModelAndView();
        mv.addObject("vipAutomaticService", config.getVipAutomaticService());
        mv.setViewName("vipAutomaticService");
        return mv;
    }

    @GetMapping("/userRemoveService")
    public ModelAndView userRemoveService( )
    {
        String json=configService.selectConfigByKey("his.agreementConfig");
        AgreementConfig config= JSONUtil.toBean(json, AgreementConfig.class);
        ModelAndView mv=new ModelAndView();
        mv.addObject("userRemoveService", config.getUserRemoveService());
        mv.setViewName("userRemoveService");
        return mv;
    }


    /**
     * @Description: 协议接口
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2026/1/9 11:31
     */
    @GetMapping("/getAgreementConfig")
    public ModelAndView getAgreementConfig(@RequestParam("appId") String appId,
                                           @RequestParam("agreementType") String agreementType) {
        ModelAndView mv = new ModelAndView();

        // 根据appId和code查询小程序用户信息
        MiniProgramAgreement agreement = miniProgramAgreementService.getOne(
                new QueryWrapper<MiniProgramAgreement>()
                        .eq("app_id", appId)
                        .eq("agreement_type", agreementType)
        );

        if (agreement != null) {
            mv.addObject("agreementBody", agreement.getAgreementContent());
            mv.addObject("agreementTitle", MiniProgramAgreementEnum.getMessage(agreementType));
            mv.setViewName("commonAgreement");
            return mv;
        } else {// 老配置 后期不用了
            String json = configService.selectConfigByKey("his.agreementConfig");
            AgreementConfig config = JSONUtil.toBean(json, AgreementConfig.class);
            MiniProgramAgreementEnum agreementEnum = MiniProgramAgreementEnum.getEnum(agreementType);

            // 修复方案
            if (agreementEnum == null) {
                mv.addObject("agreementBody", "<div class=\"error-message\"><h3>暂未找到该协议内容</h3><p>请确认协议类型是否正确，或联系管理员配置相关协议内容。</p></div>");
                mv.addObject("agreementTitle", agreementType);

                mv.setViewName("commonAgreement");
                return mv;
            }


            String data = null;
            switch (agreementEnum) {
                case USER_REGISTER:
                    data = config.getUserRegister();
                    break;
                case USER_PRIVACY:
                    data = config.getUserPrivacy();
                    break;
                case USER_HEALTH:
                    data = config.getUserHealth();
                    break;
                case VIP_SERVICE:
                    data = config.getVipService();
                    break;
                case VIP_AUTOMATIC_SERVICE:
                    data = config.getVipAutomaticService();
                    break;
                case USER_REMOVE_SERVICE:
                    data = config.getUserRemoveService();
                    break;
                case DOCTOR_REGISTER:
                    data = config.getDoctorRegister();
                    break;
                case DOCTOR_FILING:
                    data = config.getDoctorFiling();
                    break;
                default:
                    agreementType="notFound";
                    data="<div class=\"error-message\"><h3>暂未找到该协议内容</h3><p>请确认协议类型是否正确，或联系管理员配置相关协议内容。</p></div>";
                    break;
            }

            mv.addObject("agreementBody", data);
            mv.addObject("agreementTitle", MiniProgramAgreementEnum.getMessage(agreementType));
            mv.setViewName("commonAgreement");
            return mv;
        }
    }

}
