package cz.utb.tadis.attacks.util;

import cz.utb.tadis.attacks.model.Attack;
import cz.utb.tadis.attacks.model.Attacker;
import cz.utb.tadis.attacks.model.RelatedAttack;
import cz.utb.tadis.attacks.model.SoftTargetType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ExportUtil {
    public Workbook generateExport(List<Attack> attacks){
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Útoky");

        int rowNumber = 1;
        int initialColumnNumber = 1;

        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 12);

        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setBorderBottom(BorderStyle.THIN);
        headerCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

        int columnNumber = initialColumnNumber;
        String[] tableHeaders = {"Id", "Město", "Stát", "Datum", "Čas", "Popis měkkého cíle", "Teror", "Epicentrum", "Modus Operandi", "Detail Modu Operandi", "Ochranka u vstupu", "K útoku se přihlásili", "Počet útočníků", "Mrtví", "Zranění", "Potenciální oběti", "Poznámka", "Odkaz"};
        Row headerRow = sheet.createRow(rowNumber++);
        for(String tableHeader : tableHeaders){
            Cell headerCell = headerRow.createCell(columnNumber++);
            headerCell.setCellStyle(headerCellStyle);
            headerCell.setCellValue(tableHeader);
        }

        for (Attack attack: attacks) {
            Row row = sheet.createRow(rowNumber++);
            row.setHeight((short)-1);
            columnNumber = initialColumnNumber;
            row.createCell(columnNumber++).setCellValue(attack.getId());
            row.createCell(columnNumber++).setCellValue(attack.getCity());
            row.createCell(columnNumber++).setCellValue(attack.getCountry());
            row.createCell(columnNumber++).setCellValue(attack.getDate().getDayOfMonth() + "." + attack.getDate().getMonthValue() + "." + attack.getDate().getYear());
            row.createCell(columnNumber++).setCellValue(attack.getTime() != null ? attack.getTime().toString() : null);
            row.createCell(columnNumber++).setCellValue(attack.getSoftTargetDescription());
            row.createCell(columnNumber++).setCellValue(attack.getTerror() != null ? attack.getTerror().getLabel() : null);
            row.createCell(columnNumber++).setCellValue(attack.getEpicenter() != null ? attack.getEpicenter().getLabel() : null);
            row.createCell(columnNumber++).setCellValue(attack.getModusOperandi() != null ? attack.getModusOperandi().getLabel() : null);
            row.createCell(columnNumber++).setCellValue(attack.getModusOperandiDetail());
            row.createCell(columnNumber++).setCellValue(attack.getEntranceSecurity() != null ? attack.getEntranceSecurity().getLabel() : null);
            row.createCell(columnNumber++).setCellValue(attack.getTerroristOrganization());
            row.createCell(columnNumber++).setCellValue(attack.getAttackersCount() != null ? attack.getAttackersCount().toString() : "");
            row.createCell(columnNumber++).setCellValue(attack.getDeaths() != null ? attack.getDeaths().toString() : "");
            row.createCell(columnNumber++).setCellValue(attack.getInjured() != null ? attack.getInjured().toString() : "");
            row.createCell(columnNumber++).setCellValue(attack.getPotentialVictims() != null ? attack.getPotentialVictims().toString() : "");
            row.createCell(columnNumber++).setCellValue(attack.getNote());
            row.createCell(columnNumber++).setCellValue(attack.getLink());

            for(int i = initialColumnNumber; i < columnNumber; i++){
                Cell cell = row.getCell(i);
                CellStyle style = cell.getCellStyle();
                style.setWrapText(true);
                cell.setCellStyle(style);
            }
        }

        for (int i = initialColumnNumber; i < (tableHeaders.length + initialColumnNumber); i++) {
            switch(tableHeaders[i - initialColumnNumber]) {
                case "Poznámka":
                case "Odkaz":
                    sheet.setColumnWidth(i, 14000);
                    break;
                case "Popis měkkého cíle":
                case "Detail Modu Operandi":
                case "K útoku se přihlásili":
                    sheet.setColumnWidth(i, 8000);
                    break;
                default:
                    sheet.autoSizeColumn(i);
            }
        }

        Sheet softTargetTypesSheet = workbook.createSheet("Typy měkkých cílů");

        rowNumber = 1;
        columnNumber = initialColumnNumber;

        String[] sttHeaders = {"Id útoku", "Typ měkkého cíle", "Detail typu měkkého cíle"};
        headerRow = softTargetTypesSheet.createRow(rowNumber++);
        for(String tableHeader : sttHeaders){
            Cell headerCell = headerRow.createCell(columnNumber++);
            headerCell.setCellStyle(headerCellStyle);
            headerCell.setCellValue(tableHeader);
        }
        for (Attack attack: attacks) {
            for(SoftTargetType softTargetType: attack.getSoftTargetTypes()){
                Row row = softTargetTypesSheet.createRow(rowNumber++);
                columnNumber = initialColumnNumber;
                row.createCell(columnNumber++).setCellValue(attack.getId());
                row.createCell(columnNumber++).setCellValue(softTargetType.getSoftTargetType().getLabel());
                row.createCell(columnNumber++).setCellValue(softTargetType.getSoftTargetTypeDetail());
            }
        }

        for (int i = initialColumnNumber; i < (sttHeaders.length + initialColumnNumber); i++) {
            softTargetTypesSheet.autoSizeColumn(i);
        }

        Sheet attackersSheet = workbook.createSheet("Útočníci");

        rowNumber = 1;
        columnNumber = initialColumnNumber;

        String[] attackersHeaders = {"Id útoku", "Jméno", "Pohlaví", "Věk", "Státní příslušnost/rezidence", "Lokalita vzhledem k bydlišti útočníků", "Původ"};
        headerRow = attackersSheet.createRow(rowNumber++);
        for(String attackerHeader : attackersHeaders){
            Cell headerCell = headerRow.createCell(columnNumber++);
            headerCell.setCellStyle(headerCellStyle);
            headerCell.setCellValue(attackerHeader);
        }
        for (Attack attack: attacks) {
            for(Attacker attacker: attack.getAttackers()){
                Row row = attackersSheet.createRow(rowNumber++);
                columnNumber = initialColumnNumber;
                row.createCell(columnNumber++).setCellValue(attack.getId());
                row.createCell(columnNumber++).setCellValue(attacker.getName());
                row.createCell(columnNumber++).setCellValue(attacker.getGender() != null ? attacker.getGender().getLabel() : null);
                row.createCell(columnNumber++).setCellValue(attacker.getAge() != null ? attacker.getAge().toString() : "");
                row.createCell(columnNumber++).setCellValue(attacker.getResidence());
                row.createCell(columnNumber++).setCellValue(attacker.getAttackLocality() != null ? attacker.getAttackLocality().getLabel() : null);
                row.createCell(columnNumber++).setCellValue(attacker.getOrigin());
            }
        }

        for (int i = initialColumnNumber; i < (attackersHeaders.length + initialColumnNumber); i++) {
            attackersSheet.autoSizeColumn(i);
        }

        Sheet relatedAttacksSheet = workbook.createSheet("Propojené útoky");

        rowNumber = 1;
        columnNumber = initialColumnNumber;

        String[] raHeaders = {"Id útoku", "Id propojeného útoku", "Poznámka"};
        headerRow = relatedAttacksSheet.createRow(rowNumber++);
        for(String tableHeader : raHeaders){
            Cell headerCell = headerRow.createCell(columnNumber++);
            headerCell.setCellStyle(headerCellStyle);
            headerCell.setCellValue(tableHeader);
        }

        for (Attack attack: attacks) {
            for(RelatedAttack relatedAttack: attack.getRelatedAttacks()){
                Row row = relatedAttacksSheet.createRow(rowNumber++);
                columnNumber = initialColumnNumber;
                row.createCell(columnNumber++).setCellValue(attack.getId());
                row.createCell(columnNumber++).setCellValue(relatedAttack.getId());
                row.createCell(columnNumber++).setCellValue(relatedAttack.getNote());
            }
        }

        for (int i = initialColumnNumber; i < (raHeaders.length + initialColumnNumber); i++) {
            relatedAttacksSheet.autoSizeColumn(i);
        }

        return workbook;
    }
}
