import React, { Component } from 'react'
import { ScrollView, View, TouchableHighlight, Dimensions } from 'react-native'
import { LocalDate, ChronoUnit } from 'js-joda'
import Icon from 'react-native-vector-icons/Entypo'
import { secondaryColor, cycleDayColor, periodColor } from '../styles'
import {
home as labels,
bleedingPrediction as predictLabels,
shared,
} from '../i18n/en/labels'
import links from '../i18n/en/links'
import cycleModule from '../lib/cycle'
import { getFertilityStatusForDay } from '../lib/sympto-adapter'
import styles from '../styles'
import AppText from './app-text'
import DripHomeIcon from '../assets/drip-home-icons'
import Button from './button'
const ShowMoreToggler = ({ isShowingMore, onToggle }) => {
const {height, width} = Dimensions.get('window')
const leftPosition = isShowingMore ? 10 : width - 40
const style = isShowingMore ? styles.showLess : styles.showMore
const topPosition = height / 2 - styles.header.height - 30
return (
{isShowingMore ? shared.less : shared.more}
)
}
const IconText = ({ children, wrapperStyles }) => {
return (
{ children }
)
}
const HomeElement = ({ children, onPress, buttonColor, buttonLabel }) => {
return (
{ children }
)
}
export default class Home extends Component {
constructor(props) {
super(props)
const { getCycleDayNumber, getPredictedMenses } = cycleModule()
this.getCycleDayNumber = getCycleDayNumber
this.getBleedingPrediction = getPredictedMenses
this.todayDateString = LocalDate.now().toString()
const prediction = this.getBleedingPrediction()
const fertilityStatus = getFertilityStatusForDay(this.todayDateString)
this.state = {
isShowingMore: false,
cycleDayNumber: this.getCycleDayNumber(this.todayDateString),
predictionText: determinePredictionText(prediction),
bleedingPredictionRange: getBleedingPredictionRange(prediction),
...fertilityStatus
}
}
passTodayTo(componentName) {
const { navigate } = this.props
navigate(componentName, { date: LocalDate.now().toString() })
}
toggleShowingMore = () => {
this.setState({ isShowingMore: !this.state.isShowingMore })
}
render() {
const { isShowingMore, cycleDayNumber, phase, status } = this.state
const { navigate } = this.props
const cycleDayMoreText = cycleDayNumber ?
labels.cycleDayKnown(cycleDayNumber) :
labels.cycleDayNotEnoughInfo
const { statusText } = this.state
return (
this.passTodayTo('CycleDay') }
buttonColor={ cycleDayColor }
buttonLabel={ labels.editToday }
>
{cycleDayNumber || labels.unknown}
{ isShowingMore &&
{cycleDayMoreText}
}
this.passTodayTo('BleedingEditView') }
buttonColor={ periodColor }
buttonLabel={ labels.trackPeriod }
>
{this.state.bleedingPredictionRange}
{ isShowingMore &&
{this.state.predictionText}
}
navigate('Chart') }
buttonColor={ secondaryColor }
buttonLabel={ labels.checkFertility }
>
{ phase ? phase.toString() : labels.unknown }
{ phase &&
{`${labels.phase(phase)} (${status})`}
}
{ isShowingMore &&
{ `${statusText} ${links.wiki.url}.` }
}
)
}
}
function determinePredictionText(bleedingPrediction) {
if (!bleedingPrediction.length) return predictLabels.noPrediction
const todayDate = LocalDate.now()
const bleedingStart = LocalDate.parse(bleedingPrediction[0][0])
const bleedingEnd = LocalDate.parse(
bleedingPrediction[0][ bleedingPrediction[0].length - 1 ]
)
if (todayDate.isBefore(bleedingStart)) {
return predictLabels.predictionInFuture(
todayDate.until(bleedingStart, ChronoUnit.DAYS),
todayDate.until(bleedingEnd, ChronoUnit.DAYS)
)
}
if (todayDate.isAfter(bleedingEnd)) {
return predictLabels.predictionInPast(
bleedingStart.toString(), bleedingEnd.toString()
)
}
const daysToEnd = todayDate.until(bleedingEnd, ChronoUnit.DAYS)
if (daysToEnd === 0) {
return predictLabels.predictionStartedNoDaysLeft
} else if (daysToEnd === 1) {
return predictLabels.predictionStarted1DayLeft
} else {
return predictLabels.predictionStartedXDaysLeft(daysToEnd)
}
}
function getBleedingPredictionRange(prediction) {
if (!prediction.length) return labels.unknown
const todayDate = LocalDate.now()
const bleedingStart = LocalDate.parse(prediction[0][0])
const bleedingEnd = LocalDate.parse(prediction[0][ prediction[0].length - 1 ])
if (todayDate.isBefore(bleedingStart)) {
return `${todayDate.until(bleedingStart, ChronoUnit.DAYS)}-${todayDate.until(bleedingEnd, ChronoUnit.DAYS)}`
}
if (todayDate.isAfter(bleedingEnd)) {
return labels.unknown
}
return '0'
}