package org.texttechnologylab.project.gruppe_05_1.xml.speeches.Impls;

import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.LegislativePeriod;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.MemberOfParliament;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.enums.MongoDBEntryType;


import java.util.List;

public class MemberOfParliament_File_Impl extends Individual_File_Impl implements MemberOfParliament {
    private final int id;
    private String party;
    private final List<LegislativePeriod> legislativePeriods;
    private final int firstLegislativePeriodId;
    private final int lastLegislativePeriodId;
    private final String photo;

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getParty() {
        return party;
    }

    @Override
    public List<LegislativePeriod> getLegislativePeriods() {
        return legislativePeriods;
    }

    @Override
    public int getFirstLegislativePeriodId() {
        return firstLegislativePeriodId;
    }

    @Override
    public int getLastLegislativePeriodId() {
        return lastLegislativePeriodId;
    }

    @Override
    public String getPhoto() {
        return photo;
    }

    public MemberOfParliament_File_Impl(String name, String firstName, String title, String dateOfBirth, String dateofDeath, String placeOfBirth, String gender, String religion, int id, String party, List<LegislativePeriod> legislativePeriods, int firstLegislativePeriodId, int lastLegislativePeriodId, String photo) {
        super(name, firstName, title, dateOfBirth, dateofDeath, placeOfBirth, gender, religion);
        this.id = id;
        this.party = party;
        this.legislativePeriods = legislativePeriods;
        this.firstLegislativePeriodId = firstLegislativePeriodId;
        this.lastLegislativePeriodId = lastLegislativePeriodId;
        this.photo = photo;
    }

    @Override
    public boolean isCurrentMember() {
        return this.lastLegislativePeriodId == 20;
    }

    @Override
    public void updateParty(String party) {
        this.party = party;
    }

    @Override
    public MongoDBEntryType getType() {
        return MongoDBEntryType.MEMBER_OF_PARLIAMENT;
    }

    @Override
    public String toHTML() {
        StringBuilder html = new StringBuilder();

        // Basic Member details
        html.append("<div class='member-of-parliament'>")
                .append("<h2>Member of Parliament Details</h2>")
                .append("<p><strong>ID:</strong> ").append(id).append("</p>")
                .append("<p><strong>Name:</strong> ").append(getName()).append(" ").append(getFirstName()).append("</p>")
                .append("<p><strong>Party:</strong> ").append(party).append("</p>");

        // Member photo (constructed from last name and first name)
        String photoPath = "../src/memberphotos/" + getName() + "_" + getFirstName() + ".jpg";
        html.append("<p><img src='").append(photoPath).append("' alt='Photo' width='100' /></p>");

        // Legislative Periods - First and Last Period
        html.append("<h3>Legislative Periods</h3>");
        if (legislativePeriods == null || legislativePeriods.isEmpty()) {
            if (firstLegislativePeriodId == lastLegislativePeriodId) {
                html.append("<p>Member of Parliament during the legislative period ")
                        .append(firstLegislativePeriodId);

            } else {
                html.append("<p>Candidated for the first time during the legislative period ")
                        .append(firstLegislativePeriodId)
                        .append(" and the last time during the legislative period ")
                        .append(lastLegislativePeriodId);
            }
            if (isCurrentMember()) {
                html.append(" <p><b>(currently active)</b></p>");
            }
            html.append("</p>");
        } else {
            // First Legislative Period
            LegislativePeriod firstPeriod = legislativePeriods.get(0);
            html.append("<p><strong>First Period:</strong> ").append(firstPeriod.getStartDate())
                    .append(" to ").append(firstPeriod.getEndDate()).append("</p>");

            // Last Legislative Period
            LegislativePeriod lastPeriod = legislativePeriods.get(legislativePeriods.size() - 1);
            html.append("<p><strong>Last Period:</strong> ").append(lastPeriod.getStartDate())
                    .append(" to ").append(lastPeriod.getEndDate()).append("</p>");
        }

        html.append("</div>");

        return html.toString();
    }

}
