package com.izouma.nineth.service;

import com.izouma.nineth.domain.SysConfig;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.SearchMode;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.SysConfigRepo;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Service
@AllArgsConstructor
public class SysConfigService {
    private SysConfigRepo sysConfigRepo;

    public Page<SysConfig> all(PageQuery pageQuery) {
        return sysConfigRepo.findAll(JpaUtils.toSpecification(pageQuery, SysConfig.class), JpaUtils.toPageRequest(pageQuery));
    }

    public BigDecimal getBigDecimal(String name) {
        return sysConfigRepo.findByName(name).map(sysConfig -> new BigDecimal(sysConfig.getValue()))
                .orElse(BigDecimal.ZERO);
    }

    public LocalTime getTime(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        return LocalTime.from(dateTimeFormatter.parse(str));
    }

    public boolean getBoolean(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        return str.equals("1");
    }

    public int getInt(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        return Integer.parseInt(str);
    }

    @PostConstruct
    public void init() {
        if (sysConfigRepo.findByName("gift_gas_fee").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("gift_gas_fee")
                    .desc("转赠gas费")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("1")
                    .build());
        }
        if (sysConfigRepo.findByName("enable_wx_pub").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("enable_wx_pub")
                    .desc("使用公众号支付")
                    .type(SysConfig.ValueType.BOOLEAN)
                    .value("FALSE")
                    .build());
        }
        if (sysConfigRepo.findByName("enable_wx_lite").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("enable_wx_lite")
                    .desc("使用小程序支付")
                    .type(SysConfig.ValueType.BOOLEAN)
                    .value("FALSE")
                    .build());
        }
        if (sysConfigRepo.findByName("hold_days").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("hold_days")
                    .desc("持有满几天可销售")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("5")
                    .build());
        }
        if (sysConfigRepo.findByName("default_search_mode").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("default_search_mode")
                    .desc("默认搜索方式")
                    .type(SysConfig.ValueType.STRING)
                    .value("FULL")
                    .build());
        }
        if (sysConfigRepo.findByName("enable_sand_quick").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("enable_sand_quick")
                    .desc("使用衫德h5快捷支付")
                    .type(SysConfig.ValueType.BOOLEAN)
                    .value("FALSE")
                    .build());
        }
        if (sysConfigRepo.findByName("order_cancel_interval").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("order_cancel_time")
                    .desc("订单自动取消间隔(S)")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("210")
                    .build());
        }
        if (sysConfigRepo.findByName("pay_channel").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("pay_channel")
                    .desc("支付通道")
                    .type(SysConfig.ValueType.SELECT)
                    .value("sandPay")
                    .options("hmPay,sandPay")
                    .build());
        }
        if (sysConfigRepo.findByName("gift_days").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("gift_days")
                    .desc("转赠满多少天可销售")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("20")
                    .build());
        }
        if (sysConfigRepo.findByName("min_recharge_amount").isEmpty()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name("min_recharge_amount")
                    .desc("最小充值金额")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("100")
                    .build());
        }
        SearchMode searchMode = SearchMode.valueOf(sysConfigRepo.findByName("default_search_mode").get().getValue());
        JpaUtils.setDefaultSearchMode(searchMode);

        OrderPayService.setPayChannel(sysConfigRepo.findByName("pay_channel").get().getValue());
    }
}
