package com.thesis.trainingdata.features;

import com.thesis.crawlers.basic.LinksSource;
import com.thesis.crawlers.basic.ProfilesSource;
import com.thesis.sntypes.Post;
import com.thesis.sntypes.SocialProfile;
import com.thesis.storage.DbLinks;
import com.thesis.storage.DbProfiles;

import java.sql.SQLException;

public class OwnerReadersCount implements Feature {
    private LinksSource linksSource;
    private ProfilesSource profilesSource;

    public OwnerReadersCount() throws SQLException {
        linksSource = new DbLinks();
        profilesSource = new DbProfiles();
    }

    @Override
    public boolean isValid(Post post) {
        try {
            return linksSource.areReadersKnown(post.getPublicationPlace());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean isValid(Post post, SocialProfile reader) {
        return isValid(post);
    }

    @Override
    public boolean isNumber() {
        return true;
    }

    @Override
    public String description() {
        return "@ATTRIBUTE OwnerReadersCount numeric";
    }

    @Override
    public String value(Post post, SocialProfile reader) {
        try {
            return Integer.toString(profilesSource.getReadersCount(post.getPublicationPlace()));
        } catch (Exception e) {
            e.printStackTrace();
            return "?";
        }
    }
}
