"use client"

import {Greet} from "@/wailsjs/go/main/App";
import { main } from "@/wailsjs/go/models";
import React, {useState} from "react";

export default function Home() {
  const [person, setPerson] = useState(new main.Person({name: "", age: 0}));
  const [result, setResult] = useState('Please enter your name below 👇');

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>, field: string) => {
    setPerson(prevPerson => {
      const updatedPerson = {
        ...prevPerson,
        [field]: field === 'age' ? parseInt(e.target.value) : e.target.value
      };
      return {
        ...updatedPerson,
        convertValues: prevPerson.convertValues.bind(updatedPerson)
      };
    });
  };

  return <>
    name : <input type="text" autoComplete="off" value={person.name} onChange={e => handleInputChange(e, 'name')}/>
    <br/>
    <br/>
    age : <input type="number" autoComplete="off" value={person.age} onChange={e => handleInputChange(e, 'age')}/>
    <br/>
    <br/>
    {result}
    <br/>
    <br/>
    <button
      onClick={() => {
        try {
          Greet(person)
              .then(result => {
                setResult(result)

              })
              .catch(err => {
                console.error(err)
              })
        } catch (err) {
          console.error(err)
        }
      }}>
    Greet
    </button>
  </>
}
