package ru.pravvich;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import ru.pravvich.jdbc.ChannelToDatabase;
import ru.pravvich.jdbc.DatabaseConnector;
import ru.pravvich.jdbc.JDBCChannel;
import ru.pravvich.parser.*;
import ru.pravvich.timing.ByTheDayController;
import ru.pravvich.timing.CollectorDataForFirstStart;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Timer;

/**
 * Initialization state for start app.
 */
class InitializerStartStatement {
    /**
     * Channel to database with automatic himself support.
     */
    private ChannelToDatabase channelDB;
    /**
     * Queue which temp cash before addition data in database.
     */
    private Queue<Proposal> proposals;
    /**
     * Parser html pages to Proposal object.
     */
    private ParserSingePage parser;
    /**
     * Break page, generate next url page of website.
     */
    private PageBreaker breaker;
    /**
     * For cycle repeat collecting data.
     */
    private Timer timer;

    /**
     * Default constructor.
     */
    InitializerStartStatement() {

        proposals = new LinkedList<>();

        timer = new Timer();

        initParser();

        initChannelToDB();

        initPageBreaker();
    }

    /**
     * Start app.
     *
     * @param period between collection data.
     */
    void start(long period) {

        if (channelDB.firstStart()) {

            BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%m%n")));

            timer.schedule(new CollectorDataForFirstStart(proposals, breaker, parser, channelDB), 0);

            startCycleCollectData(86_400_000L, period);

        } else {

            startCycleCollectData(0, period);

        }

    }

    /**
     * Cycle collecting data.
     *
     * @param delay  before next collecting data in milliseconds.
     * @param period between collection data in milliseconds.
     */
    private void startCycleCollectData(long delay, long period) {
        timer.schedule(
                new ByTheDayController(proposals, breaker, parser, channelDB),
                delay, period);
    }

    private void initChannelToDB() {
        channelDB = new JDBCChannel(
                new DatabaseConnector(
                        new PropertiesLoader(
                                "DBAction")
                )
        );
    }

    private void initParser() {
        parser = new ParserSingePage(
                proposals,
                new TimeFormatConverter(),
                new ProposalValidatorUtil()
        );
    }

    private void initPageBreaker() {
        breaker = new PageBreaker(
                new PropertiesLoader("siteURL")
        );
    }
}

@@ -1142,24 +1142,55 @@
 tement() %7B%0A%0A
+        initConfigForLog4j();%0A%0A
         prop
@@ -1213,24 +1213,24 @@
 edList%3C%3E();%0A
-
 %0A        tim
@@ -1494,97 +1494,8 @@
  %7B%0A%0A
-            BasicConfigurator.configure(new ConsoleAppender(new PatternLayout(%22%25m%25n%22)));%0A
 %0A   
@@ -1708,32 +1708,32 @@
 ta(0, period);%0A%0A
-
         %7D%0A%0A    %7D
@@ -1726,32 +1726,504 @@
       %7D%0A%0A    %7D%0A%0A
+    /**%0A     * Initialization rules for logger.%0A     */%0A    private void initConfigForLog4j() %7B%0A%0A        BasicConfigurator.configure(%0A                new ConsoleAppender(%0A                        new PatternLayout(%0A                                new PropertiesLoader(%22log4j%22).getValue(%0A                                        %22log4j.appender.stdout.layout.conversionPattern%22%0A                                )%0A                        )%0A                )%0A        );%0A%0A    %7D%0A%0A
     /**%0A     * C

Complete parser