package com.bocloud.ims.booter.utils;

import com.bocloud.ims.entity.resource.azure.price.AzureResourcePrice;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelReader {

    private static Logger logger = LoggerFactory.getLogger(ExcelReader.class);

    public List<AzureResourcePrice> getPrices(InputStream inputStream) {
        List<AzureResourcePrice> listMap = new ArrayList<>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                AzureResourcePrice price = new AzureResourcePrice();
                HSSFRow row = sheet.getRow(i);
                String productType = row.getCell(0).getStringCellValue();
                String resourceType = row.getCell(1).getStringCellValue();
                String regionId = row.getCell(2).getStringCellValue();
                row.getCell(3).setCellType(CellType.STRING);
                String unitPriceStr = row.getCell(3).getStringCellValue();
                String name = row.getCell(4).getStringCellValue();
                row.getCell(5).setCellType(CellType.STRING);
                String resourceSize = row.getCell(5).getStringCellValue();
                String period = row.getCell(6).getStringCellValue();

                price.setProductType(productType);
                price.setResourceType(resourceType);
                price.setRegionId(regionId);
                BigDecimal unitPrice = new BigDecimal(unitPriceStr);
                price.setUnitPrice(unitPrice);
                if (!StringUtils.isEmpty(name)) {
                    price.setName(name);
                }
                if (!StringUtils.isEmpty(resourceSize)) {
                    price.setResourceSize(Integer.valueOf(resourceSize));
                }
                price.setPeriod(period);
                listMap.add(price);
            }
            return listMap;
        } catch (IOException e) {
            logger.info("定价信息转换失败" + e.getMessage());
            logger.error("定价信息转换失败:", e);
            return listMap;
        }
    }

    public List<Map<String, String>> getFlavor(InputStream inputStream) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            List<Map<String, String>> listMap = new ArrayList<>();
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Map<String, String> map = new HashMap<>();
                HSSFRow row = sheet.getRow(i);
                String name = row.getCell(0).getStringCellValue();
//                String regionId = row.getCell(1).getStringCellValue();
                map.put("name", name);
//                map.put("regionId", regionId);
                listMap.add(map);
            }
            return listMap;
        } catch (IOException e) {
            logger.info("规格信息转换失败" + e.getMessage());
            return null;
        }

    }

//    public static void main(String[] args) {
//        try {
//            File file = new File("D:\\price.xls");
//            InputStream inputStream= new FileInputStream(file);
//            List<AzureResourcePrice> prices = getPrices(inputStream);
//            System.out.println("args = [" + args + "]");
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//    }

}
