package com.example.demo.controller;
import com.example.demo.domain.Idn;
import com.example.demo.domain.Idn2;
import com.example.demo.service.IdnService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

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

@Controller
public class WebappController
{
    private final IdnService idnService;

    public WebappController(IdnService idnService)
    {
        this.idnService = idnService;
    }
    @GetMapping("/tools")
    public String index(Model model) {
        //hardcode single get for now
       // Idn id1 = idnService.getIdn(1);
       // byte[] imageByte1 = id1.getImage();
      //  String photo1 = Base64.getEncoder().encodeToString(imageByte1);
       // imag1 = "data:image/png;base64," + photo1;
        //ArrayList<Idn> idnlist = idnService.getAll();    //get all tools :D
        ArrayList<Idn2> imglist = new ArrayList<Idn2>();
        ArrayList<Idn>   idnlist = idnService.getIdnByType(5);
        String imag1 = "";
        for (int i = 0; i < idnlist.size(); i++)
        {
            byte[] img = idnlist.get(i).getImage();
            if (img != null)
            {
                String photo = Base64.getEncoder().encodeToString(img);
                imag1 = "data:image/png;base64," + photo;
            }
            Idn2 idn2 = new Idn2();
            idn2.id = idnlist.get(i).getId();
            idn2.image_str = imag1;
            idn2.date_created = idnlist.get(i).getCreationDate().toString();
            idn2.date_modified = idnlist.get(i).getLastModificationDate().toString();
            imglist.add(idn2);
        }
        model.addAttribute("idn_list", idnlist);
        model.addAttribute("img_list", imglist);

        return "index";
    }
}
