package com.m2sdl.undercooked.domain;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.mapsforge.core.model.LatLong;

import java.util.ArrayList;
import java.util.HashMap;


public class Restaurant {

    /*
     * nom
     * type (catégorie)
     * horaire d'ouverture
     * adresse
     * type de cuisine
     * téléphone
     * website*/
    private String name;
    private String categorie;
    private String horaires;
    private String adresse;
    private String typeDeCuisine;
    private String telephone;
    private String website;
    private LatLong position;

    public Restaurant() {
        name = "Pas de données disponibles pour ce restaurant :-(";
    }

    public LatLong getPosition() {
        return position;
    }

    public void setPosition(LatLong position) {
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategorie() {
        return categorie;
    }

    public void setCategorie(String categorie) {
        this.categorie = categorie;
    }

    public String getHoraires() {
        return horaires;
    }

    public void setHoraires(String horaires) {
        this.horaires = horaires;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getTypeDeCuisine() {
        return typeDeCuisine;
    }

    public void setTypeDeCuisine(String typeDeCuisine) {
        this.typeDeCuisine = typeDeCuisine;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    /*
     * parse the String of the form:
     * key:detail=value\n
     * */
    public void parseData(String dataFromSql) {
        String[] lines = dataFromSql.split(
                "\r"
        );
        var dataPoints = new HashMap<String, String>();
        for (String line : lines) {
            var keyval = line.split("=");
            if (keyval.length >= 2) {
                dataPoints.put(keyval[0], keyval[1]);
            }
        }
        name = dataPoints.getOrDefault("name", "Unknown name");
        horaires = dataPoints.getOrDefault("opening_hours", "Horaires Inconnus");
        String houseNumber = dataPoints.get("addr:housenumber");
        String street = dataPoints.get("addr:street") == null ? dataPoints.get("addr:place") : dataPoints.get("addr:street");
        if (houseNumber == null || street == null) {
            adresse = "Adresse inconnue";
        } else {
            adresse = String.format("%s %s", houseNumber, street);
        }
        typeDeCuisine = String.format("Spécialité: %s",
                dataPoints.getOrDefault("cuisine", "Type de Cuisine inconnu")
        );
        telephone = dataPoints.getOrDefault("phone", "Téléphone Inconnu");
        website = dataPoints.getOrDefault("website", "Site internet inconnu");
    }

    @SuppressLint("SetTextI18n")
    public void addInfoToLayout(Context ctx, LinearLayout layout) {
        TextView title = new TextView(ctx);
        title.setText(name);
        title.setTextSize(22f);
        title.setPadding(0, 0, 0, 12);
        title.setTypeface(null, android.graphics.Typeface.BOLD);
        layout.addView(title);

        if (categorie != null && !categorie.isEmpty()) {
            TextView cat = new TextView(ctx);
            cat.setText("Catégorie : " + categorie);
            cat.setPadding(0, 0, 0, 8);
            layout.addView(cat);
        }

        TextView addr = new TextView(ctx);
        addr.setText("📍 " + adresse);
        addr.setPadding(0, 0, 0, 12);
        layout.addView(addr);

        TextView cuisineTv = new TextView(ctx);
        cuisineTv.setText("🍽️ " + typeDeCuisine);
        cuisineTv.setPadding(0, 0, 0, 8);
        layout.addView(cuisineTv);

        TextView horairesTv = new TextView(ctx);
        horairesTv.setText("🕒 " + horaires);
        horairesTv.setPadding(0, 0, 0, 12);
        layout.addView(horairesTv);

        TextView contactTitle = new TextView(ctx);
        contactTitle.setText("Contact");
        contactTitle.setTypeface(null, android.graphics.Typeface.BOLD);
        contactTitle.setPadding(0, 8, 0, 8);
        layout.addView(contactTitle);

        if (telephone != null && !telephone.isEmpty()) {
            TextView phoneTv = new TextView(ctx);
            phoneTv.setText("📞 " + telephone);
            phoneTv.setTextIsSelectable(true);
            layout.addView(phoneTv);
        }

        if (website != null && !website.isEmpty()) {
            TextView webTv = new TextView(ctx);
            webTv.setText("🌐 " + website);
            webTv.setTextIsSelectable(true);
            layout.addView(webTv);
        }
    }
}
