package com.bocloud.ims.service.resource.azure.slb;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.azure.slb.InboundNatPool;
import com.bocloud.ims.interfaces.resource.azure.slb.InboundNatPoolService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.azure.slb.InboundNatPoolRepository;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service("inboundNatPoolService")
public class InboundNatPoolServiceImpl implements InboundNatPoolService {

    private static Logger logger = LoggerFactory.getLogger(InboundNatPoolServiceImpl.class);
    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private InboundNatPoolRepository inboundNatPoolRepository;
    @Autowired
    private LockFactory lockFactory;

    @Override
    public BocloudResult<GridBean<InboundNatPool>> list(Pager pager, RequestContext context) {
        List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
        Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
        sorter.put("gmtCreate", Common.ONE);
        Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            List<InboundNatPool> list = inboundNatPoolRepository.list(page, rows, params, sorter);
            int total = inboundNatPoolRepository.count(params);
            GridBean<InboundNatPool> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list natPool failure:", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<Void> create(InboundNatPool entityParams, RequestContext context) {
        return new BocloudResult<>(false, "暂不支持");
    }

    @Override
    public BocloudResult<Void> remove(Long id, RequestContext context) {
        return new BocloudResult<>(false, "暂不支持");
    }

    @Override
    public BocloudResult<InboundNatPool> detail(Long id, RequestContext context) {
        try {
            InboundNatPool natPool = inboundNatPoolRepository.query(id);
            Assert.notNull(natPool, "资源不存在");
            return new BocloudResult<>(true, natPool, "查询详情成功");
        } catch (Exception e) {
            logger.error("query natPool detail fail:", e);
            return new BocloudResult<>(false, "查询详情失败:" + e.getMessage());
        }
    }
}
