import { StyleSheet, Text, View } from "@react-pdf/renderer";
import React from "react";
import { Simulation } from "../../../../../simulations/models/Simulation";
import MarketAnalystCard from "./MarketAnalystCard";

const styles = StyleSheet.create({
    section: {
        width: "100%",
        alignItems: "flex-end",
        margin: "0 20px",
        marginBottom: "10px",
        padding: "0 20px 5px 20px",
        display: "flex",
        fontFamily: "Times-Bold",
        justifyContent: "space-between",
        flexDirection: "row",
        fontSize: "8px",
    },
    firstLine: {
        display: "flex",
        flexDirection: "row",
        position: "relative",
        width: "100%",
        textAlign: "center",
        justifyContent: "space-between",
    },
    motoPart: {
        display: "flex",
        flexDirection: "row",
        position: "relative",
        justifyContent: "center",
        flexWrap: "wrap",
        width: "100%",
    },
});

const NeighborhoodAnalysisPDF = ({
    simulation,
}: {
    simulation?: Simulation;
}) => {
    const existing = simulation?.existingDescription;
    const addressData = simulation?.addressData;

    const hasNeighborhoodName =
        (existing?.neighborhood_name ?? "").toString().trim().length > 0 ||
        (addressData?.neighborhood_name ?? "").toString().trim().length > 0;
    const hasNeighborhoodAvgIncome =
        existing?.neighborhood_average_income !== undefined &&
        existing?.neighborhood_average_income !== null;
    const hasNeighborhoodVacant =
        existing?.neighborhood_vacant_housing !== undefined &&
        existing?.neighborhood_vacant_housing !== null;
    const hasNeighborhoodRentalTension =
        (existing?.neighborhood_rental_tension ?? "").toString().trim().length >
        0;
    const hasNeighborhoodComment =
        (existing?.neighborhood_comment ?? "").toString().trim().length > 0 ||
        (addressData?.neighborhood_comment ?? "").toString().trim().length > 0;
    const hasNeighborhoodPopulation =
        (existing?.neighborhood_population ?? "").toString().trim().length >
            0 ||
        (addressData?.neighborhood_population ?? "").toString().trim().length >
            0;

    const hasNeighborhoodData =
        hasNeighborhoodName ||
        hasNeighborhoodAvgIncome ||
        hasNeighborhoodVacant ||
        hasNeighborhoodRentalTension ||
        hasNeighborhoodComment ||
        hasNeighborhoodPopulation;

    if (!hasNeighborhoodData) {
        return null;
    }

    const neighborhoodName =
        existing?.neighborhood_name || addressData?.neighborhood_name;
    const neighborhoodComment =
        existing?.neighborhood_comment || addressData?.neighborhood_comment;
    const neighborhoodPopulation =
        existing?.neighborhood_population ||
        addressData?.neighborhood_population;

    return (
        <View
            wrap
            style={{
                ...styles.section,
            }}
        >
            <View
                wrap
                style={{
                    ...styles.motoPart,
                    width: "100%",
                }}
            >
                <View
                    style={{
                        color: "#895cf5",
                        fontSize: 16,
                        width: "100%",
                        marginBottom: 12,
                    }}
                >
                    <Text>Analyse du marché - analyse du quartier</Text>
                </View>

                <View
                    style={{
                        border: "1px solid #E6EAEF",
                        borderRadius: "2px",
                        width: "100%",
                        paddingRight: 6,
                        paddingLeft: 6,
                        paddingTop: 5,
                        paddingBottom: 5,
                        marginBottom: 10,
                    }}
                >
                    {hasNeighborhoodName && (
                        <View
                            style={{
                                color: "#895cf5",
                                fontSize: 12,
                                width: "100%",
                                marginBottom: 8,
                            }}
                        >
                            <Text>À propos de {neighborhoodName}</Text>
                        </View>
                    )}

                    {(hasNeighborhoodComment || hasNeighborhoodPopulation) && (
                        <View
                            style={{
                                fontSize: 9,
                                width: "100%",
                                marginBottom: 12,
                                fontFamily: "Times-Roman",
                            }}
                        >
                            {hasNeighborhoodComment && (
                                <Text style={{ marginBottom: 6 }}>
                                    {neighborhoodComment}
                                </Text>
                            )}
                            {hasNeighborhoodPopulation && (
                                <Text>{neighborhoodPopulation}</Text>
                            )}
                        </View>
                    )}

                    <View
                        style={{
                            ...styles.firstLine,
                            marginVertical: 8,
                            gap: 8,
                        }}
                    >
                        {hasNeighborhoodAvgIncome && (
                            <MarketAnalystCard
                                label={"Revenu moyen dans le quartier"}
                                value={`${existing?.neighborhood_average_income?.toLocaleString()} €/an`}
                            />
                        )}
                        {hasNeighborhoodVacant && (
                            <MarketAnalystCard
                                label={"Logements vacants"}
                                value={`${parseFloat(existing?.neighborhood_vacant_housing?.toString() ?? "0").toFixed(1)}%`}
                            />
                        )}
                        {hasNeighborhoodRentalTension && (
                            <MarketAnalystCard
                                label={"Tension locative"}
                                value={
                                    existing?.neighborhood_rental_tension as string
                                }
                                star={
                                    parseInt(
                                        (
                                            existing?.neighborhood_rental_tension?.toString() ??
                                            ""
                                        ).split("/")[0],
                                    ) || 0
                                }
                            />
                        )}
                        <View></View>
                    </View>
                </View>
            </View>
        </View>
    );
};

export default NeighborhoodAnalysisPDF;
