package com.bustiblelemons.cthulhator.character.history.logic;

import android.content.Context;
import android.support.annotation.NonNull;

import com.bustiblelemons.cthulhator.R;
import com.bustiblelemons.cthulhator.character.characterslist.model.SavedCharacter;
import com.bustiblelemons.cthulhator.character.history.model.HistoryEvent;
import com.bustiblelemons.cthulhator.settings.Settings;
import com.bustiblelemons.cthulhator.settings.character.CharacterSettings;
import com.bustiblelemons.cthulhator.system.brp.statistics.BRPStatistic;
import com.bustiblelemons.randomuserdotme.model.Location;

import org.joda.time.DateTime;

import java.util.Random;

/**
 * Created by hiv on 13.11.14.
 */
public class HistoryEventFactory {

    private static HistoryEventFactory INSTANCE;
    private        Context             mContext;
    private        SavedCharacter      mSavedCharacter;

    public HistoryEventFactory(Context context) {
        mContext = context;
    }

    public static HistoryEventFactory from(@NonNull Context context) {
        if (INSTANCE == null) {
            INSTANCE = new HistoryEventFactory(context);
        } else if (INSTANCE != null) {
            if (INSTANCE.mContext != null && !INSTANCE.mContext.equals(context)) {
                return INSTANCE = new HistoryEventFactory(context);
            }
        }
        return INSTANCE;
    }

    public HistoryEventFactory withCharacter(SavedCharacter mSavedCharacter) {
        this.mSavedCharacter = mSavedCharacter;
        return this;
    }

    public HistoryEvent buildBirthEvent() {
        DateTime dateTime = getBirthDateTime();
        HistoryEvent birthEvent = new HistoryEvent();
        birthEvent.setDate(dateTime.getMillis());
        if (mSavedCharacter != null && mSavedCharacter.getDescription() != null) {
            Location birthLocation = mSavedCharacter.getDescription().getLocation();
            if (birthLocation != null) {
                birthEvent.setLocation(birthLocation);
                String name = mContext.getResources().getString(R.string.born);
                String description = birthLocation.toString();
                birthEvent.setName(name);
                birthEvent.setDescription(description);
            }
        }
        return birthEvent;
    }

    private DateTime getBirthDateTime() {
        CharacterSettings s = Settings.getLastPortraitSettings(mContext);
        int defaultYear = s.getCthulhuPeriod().getDefaultYear();
        int edu = 4;
        if (mSavedCharacter != null) {
            mSavedCharacter.getStatisticValue(BRPStatistic.EDU.name());
        }
        int suggestedAge = edu + 6;
        int estimateYear = defaultYear - suggestedAge;
        Random r = new Random();
        int month = r.nextInt(12);
        int day = r.nextInt(27);
        int h = r.nextInt(23);
        return new DateTime(estimateYear, month, day, h, 0);
    }
}

@@ -1721,24 +1721,130 @@
 tMillis());%0A
+        String name = mContext.getResources().getString(R.string.born);%0A        birthEvent.setName(name);%0A
         if (
@@ -2122,167 +2122,45 @@
 ing 
-name = mContext.getResources().getString(R.string.born);%0A                String description = birthLocation.toString();%0A                birthEvent.setName(name
+description = birthLocation.toString(
 );%0A 

add default localized title for borndate