package eu.hsrw.ias.ggd;

import Credentials.PlonkTest;
import Credentials.UserCredentials;
import Files.ReadExcelFile;
import Files.UnZipFile;
import HttpCall.PlonkHttpGet;

import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PlonkDataRequest {
    public static String x() throws Exception {
        String token;
        String result;
        {
            PlonkTest plonkTest = new PlonkTest();
            PlonkHttpGet plonkHttpGet = new PlonkHttpGet();
            String outputToken = PlonkHttpPostRequest.OnCallMethod();
            JSONObject obj = new JSONObject(outputToken);
            token = obj.getString("access_token");
            String urlExtension= plonkTest.getServer()+"/api/pre/   ubersicht/data/Gießen-Düngen%20(Stufen)";
            result = plonkHttpGet.PlonkHttpGetCall(urlExtension,token);

        }

        return result;
    }

    public static void main(String[] args) {
        //if we want to fetch data from PlonkApi

//        try {
//            String output = x();
//            System.out.println(output);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //This part is unziping the excel file from the url
        UnZipFile unZipFile = new UnZipFile();
        UserCredentials userCredentials = new UserCredentials();
        String workPath = userCredentials.getHomeRoot();

        Path source = Paths.get(workPath+"PlonkData.zip");
        Path target = Paths.get(workPath);
        ReadExcelFile DT = new ReadExcelFile();

        try {

            unZipFile.unzipFolder(source, target);
            DT.readExcel();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
