package ch.unisg.tapas.auctionhouse.adapter.out.web;

import ch.unisg.tapas.auctionhouse.application.port.out.AuctionHouseDiscoveryPort;
import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class AuctionHouseDiscoveryHttpAdapter implements AuctionHouseDiscoveryPort {
    private static final Logger LOGGER = LogManager.getLogger(AuctionHouseDiscoveryHttpAdapter.class);

    public List<AuctionHouseDiscoveryInformation> fetchAuctionHouseInformation(URI auctionHouseURI){

        System.out.println(auctionHouseURI);

        try{
            var client = HttpClient.newHttpClient();
            var request = HttpRequest.newBuilder()
                .uri(auctionHouseURI)
                .GET()
                .build();
            var response = client.send(request, HttpResponse.BodyHandlers.ofString());
            LOGGER.info(response.body());
            var responseBody = new JSONObject(response.body());

            var arrayOfInformation = responseBody.getJSONArray("auctionHouseInfo");
            var returnList = new LinkedList<AuctionHouseDiscoveryInformation>();

            for(int i = 0; i < arrayOfInformation.length(); i++)
            {
                var informationJSON = arrayOfInformation.getJSONObject(i);
                var information = new AuctionHouseDiscoveryInformation(
                    new AuctionHouseDiscoveryInformation.AuctionHouseUri(URI.create(informationJSON.getString("auctionHouseURI"))),
                    new AuctionHouseDiscoveryInformation.WebSubUri(URI.create(informationJSON.getString("webSubURI"))),
                    new AuctionHouseDiscoveryInformation.TaskTypes(getTaskTypes(informationJSON.getJSONArray("taskTypes"))),
                    new AuctionHouseDiscoveryInformation.TimeStamp(Timestamp.valueOf(informationJSON.getString("timeStamp"))),
                    new AuctionHouseDiscoveryInformation.GroupName(informationJSON.getString("groupName"))
                );
                returnList.add(information);
            }

            return returnList;

        } catch (IOException e) {
            e.printStackTrace();
            return Collections.<AuctionHouseDiscoveryInformation>emptyList();
        } catch (InterruptedException e) {
            e.printStackTrace();
            return Collections.<AuctionHouseDiscoveryInformation>emptyList();
        }

    }

    private List<String> getTaskTypes(JSONArray arrayOfTypes){
        var listOfTypes = new LinkedList<String>();

        for(int i = 0; i < arrayOfTypes.length(); i++)
        {
            listOfTypes.add(arrayOfTypes.getString(i));
        }

        return listOfTypes;
    }

}
