package com.utic.center.utic.stat.server.service.partition;

import com.utic.center.utic.stat.server.dao.repository.UticStatTableManagerRepository;
import com.utic.center.utic.stat.server.dto.TableInfoDto;
import com.utic.center.utic.stat.server.dto.TableExistDto;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class PatternHist extends MakeTablePartition {

    private final List<TableInfoDto> patnTables = new ArrayList<>();

    private final String table05MinPrefix = "PATN_";
    private final String table15MinPrefix = "PATN_15MIN_";

    private final String data05MinSpace = "PATN_DATA";
    private final String indx05MinSpace = "PATN_INDX";
    private final String data15MinSpace = "PATN_15MIN_DATA";
    private final String indx15MinSpace = "PATN_15MIN_INDX";

    public PatternHist(UticStatTableManagerRepository repo,
                       String crtMonth,
                       String commentCrtMonth) {
        super(repo, crtMonth, commentCrtMonth);

        this.patnTables.add(new TableInfoDto("HOLIDAY", "휴일"));
        this.patnTables.add(new TableInfoDto("MON", "월요일"));
        this.patnTables.add(new TableInfoDto("SAT", "토요일"));
        this.patnTables.add(new TableInfoDto("SUN", "일요일"));
        this.patnTables.add(new TableInfoDto("FRI", "금요일"));
        this.patnTables.add(new TableInfoDto("WEEK", "평일"));
    }

    @Override
    public boolean createPartitionTable() {
        for (TableInfoDto patnTable : this.patnTables) {
            // 05분 패턴 이력
            final String table05MinHist = this.table05MinPrefix + patnTable.getTable() + "_" + this.crtMonth;
            final String table05MinHistDesc = "링크패턴_" + patnTable.getDesc() + "_이력 " + this.commentCrtMonth;
            if (!createTable(table05MinHist, table05MinHistDesc, this.data05MinSpace, this.indx05MinSpace)) {
                return false;
            }

            // 15분 패턴 이력
            final String table15MinHist = this.table15MinPrefix + patnTable.getTable() + "_" + this.crtMonth;
            final String table15MinHistDesc = "링크패턴15분_" + patnTable.getDesc() + "_이력 " + this.commentCrtMonth;
            if (!createTable(table15MinHist, table15MinHistDesc, this.data15MinSpace, this.indx15MinSpace)) {
                return false;
            }
        }
        return true;
    }

    private boolean createTable(String tableName, String tableComment, String dataSpace, String indexSpace) {
        TableExistDto existDto = this.repo.findTableExist(tableName);
        if (existDto == null) {
            return false;
        }
        if (existDto.getTableExist() > 0) {
            return true;
        }

        if (this.repo.createPatnHistTable(tableName, dataSpace) < 0) {
            return false;
        }
        if (this.repo.addPatnHistPrimaryKey(tableName, indexSpace) < 0) {
            return false;
        }
        addComments(tableName, tableComment);
        return true;
    }

    private void addComments(String tableName, String tableComment) {
        this.repo.addTableComment(tableName, tableComment);

        this.repo.addColumnComment(tableName, "patn_date", "패턴일시_일");
        this.repo.addColumnComment(tableName, "patn_time", "패턴시간");
        this.repo.addColumnComment(tableName, "linkid", "링크ID");
        this.repo.addColumnComment(tableName, "speed", "속도");
        this.repo.addColumnComment(tableName, "log_date", "저장시간");
        this.repo.addColumnComment(tableName, "linklevel", "링크레벨");
    }

}
