package com.njcn.msgpush.module.push.controller.admin.channel;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.controller.admin.channel.vo.ChannelProviderConfigReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.service.channel.ChannelProviderConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "管理后台 - 渠道服务商")
@Slf4j
@Validated
@RestController
@RequestMapping("/push/channel")
public class ChannelProviderConfigController {

    @Autowired
    private ChannelProviderConfigService channelProviderConfigService;

    @Autowired
    @Qualifier("messageProviderFactoryMap")
    private Map<String, MessageProviderFactory> messageProviderFactoryMap;


    @PostMapping("/list")
    @Operation(summary = "分页查询渠道服务商列表")
    @PreAuthorize("@ss.hasPermission('push:channel:list')")
    public CommonResult<Page<ChannelProviderConfigDO>> getChannelProviderConfigPage(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        Page<ChannelProviderConfigDO> res = channelProviderConfigService.listChannelProviderCfg(reqVO);
        return CommonResult.success(res);
    }

    @PostMapping("/add")
    @Operation(summary = "新增渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:add')")
    public CommonResult<Boolean> addChannelProvider(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        boolean res = channelProviderConfigService.save(BeanUtil.copyProperties(reqVO, ChannelProviderConfigDO.class));
        return CommonResult.success(res);
    }

    @PostMapping("/update")
    @Operation(summary = "更新渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:update')")
    public CommonResult<Boolean> updateChannelProvider(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        boolean res = channelProviderConfigService.updateById(BeanUtil.copyProperties(reqVO, ChannelProviderConfigDO.class));
        return CommonResult.success(res);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:delete')")
    public CommonResult<Boolean> deleteChannelProvider(@RequestBody List<String> ids) {
        boolean res = channelProviderConfigService.removeBatchByIds(ids);
        return CommonResult.success(res);
    }

    @GetMapping("/toggle")
    @Operation(summary = "启用/禁用渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:toggle')")
    public CommonResult<Boolean> toggleEnableChannelProvider(@RequestParam("id") String id) {
        boolean res = channelProviderConfigService.toggleEnableField(id);
        return CommonResult.success(res);
    }

    /**
     * 添加指定providerType服务商对应的bean
     *
     * @param providerType 服务商类型，例如：aliyun\telecom\UniPush
     */
    public void registerProviderBean(String providerType) {
//        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
//        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClass);
//        for (Object arg : args) {
//            builder.addConstructorArgValue(arg);
//        }
//        beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition());

    }

    /**
     * 移除指定providerType服务商对应的bean
     *
     * @param providerType 服务商类型，例如：aliyun\telecom\UniPush
     */
    public void removeProviderBean(String providerType) {
//        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
//        if (beanFactory.containsBeanDefinition(beanName)) {
//            beanFactory.removeBeanDefinition(beanName);
//        }
        messageProviderFactoryMap.remove(providerType);
    }
}
