package eu.dnetlib.data.claims.utils;

import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class CommunityUtils {
    private static String communityAPI;
    String name;
    List<String> managers = new ArrayList<String>();
    private final static Logger log = LogManager.getLogger(CommunityUtils.class);

    public String getCommunityAPI() { return communityAPI; }
    public void setCommunityAPI(String communityAPI) { CommunityUtils.communityAPI = communityAPI; }

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

    public List<String> getManagers() {
        return managers;
    }
    public void setManagers(List<String> managers) {
        this.managers = managers;
    }

    public  CommunityUtils getCommunityInfo(String community) {
        String url = communityAPI + community;
        URL obj = null;
        String responseStr = null;
        log.debug("Community info url is " + url);

        try {
            obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            log.debug("User info response code is: " + con.getResponseCode());
            if (con.getResponseCode() != 200) {
                return null;
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuffer response = new StringBuffer();
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine).append("\n");
            }
            in.close();
            responseStr = response.toString();
        } catch (Exception e) {
            log.error("An error occured while trying to fetch user info ", e);
            return null;
        }
        return json2CommunityInfo(responseStr);
    }
    private CommunityUtils json2CommunityInfo(String json){

        log.debug("Try to create CommunityInfo class from json: "+json);
        if (json == null){
            return null;
        }

        BufferedReader br = new BufferedReader(new StringReader(json));
        //convert the json string back to object
        Gson gson = new Gson();
        CommunityUtils communityInfo = null;
        try {
            communityInfo = gson.fromJson(br, CommunityUtils.class);
        }catch(Exception e){
            log.debug("Error in parsing json response. Given json is : "+json, e);
            return null;
        }

        log.debug("Original response.........: "+communityInfo.toString());



        return communityInfo;
    }
    public Boolean isCommunityManager(String communityId, String userMail){
        CommunityUtils community = this.getCommunityInfo(communityId);
        return (community!=null && community.managers.indexOf(userMail) !=-1);
    }
}
