package com.moviesdb;

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

import java.io.*;
import java.util.Objects;
import java.util.Properties;


public class ConnectionParams {
    private String host;
    private String user;
    private String password;
    private boolean initilizeErr;
    private List<String> initilizeErrCause;

    public ConnectionParams(String host, String user, String password){
        try {
            this.host = Objects.requireNonNull(host);
            this.user = Objects.requireNonNull(user);
            this.password = Objects.requireNonNull(password);
            initilizeErr = false;
        } catch (NullPointerException ex){
            ex.printStackTrace();
            initilizeErr = true;
            initilizeErrCause = new ArrayList<String>();
            if (Objects.isNull(host)) initilizeErrCause.add("host");
            if (Objects.isNull(user)) initilizeErrCause.add("user");
            if (Objects.isNull(password)) initilizeErrCause.add("password");
        }
    }

    public ConnectionParams(String filePath){
        try {
            FileInputStream fis;
            Properties property = new Properties();

            fis = new FileInputStream(filePath);
            property.load(fis);

            initilizeErrCause = new ArrayList<String>();
            if (Objects.isNull(property.getProperty("CONFIG_DEFAULT_HOST"))) initilizeErrCause.add("host");
            if (Objects.isNull(property.getProperty("CONFIG_DEFAULT_USER"))) initilizeErrCause.add("user");
            if (Objects.isNull(property.getProperty("CONFIG_DEFAULT_PASS"))) initilizeErrCause.add("password");

            initilizeErr = !initilizeErrCause.isEmpty();

            if (!initilizeErr) {
                this.host = property.getProperty("CONFIG_DEFAULT_HOST");
                this.user = property.getProperty("CONFIG_DEFAULT_USER");
                this.password = property.getProperty("CONFIG_DEFAULT_PASS");
            }

        } catch (IOException ex) {
            ex.printStackTrace();
            initilizeErr = true;
            initilizeErrCause = new ArrayList<String>();
            initilizeErrCause.add("filePath");
        }
    }

    public boolean initilized(){
        return !initilizeErr;
    }

    public List<String> getInitilizeErrCause(){
        if (initilizeErr)
            return initilizeErrCause;
        else
            return null;
    }
    
    public String host(){
        return host;
    }
    
    public String user(){
        return user;
    }
    
    public String password() {
        return password;
    }
 }
