import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
  Platform,
  StyleSheet,
  Text,
  View,
  ScrollView,
  FlatList,
  TouchableOpacity,
  Image
} from 'react-native'
import CommonFn from './commonFn'
import moment from 'moment'
import styles from './styles/CalendarSelectStyles'
import _ from 'lodash'
import MCIcons from 'react-native-vector-icons/MaterialCommunityIcons'

class CalendarSelect extends Component {
  constructor(props) {
    super(props)
    this.state = {
      viewMode: 'day'
    }
  }

  renderDay(day) {
    const { calendarMonth, date, warpDayStyle, dateSelectedWarpDayStyle,
      renderChildDay, textDayStyle, currentDayStyle, notCurrentDayOfMonthStyle } = this.props
    const isCurrentMonth = calendarMonth === CommonFn.ym()
    const isCurrent = isCurrentMonth && CommonFn.ymd() === day
    const dateSelected = date && CommonFn.ymd(date) === day
    const notCurrentMonth = day.indexOf(calendarMonth) !== 0

    return (
      <TouchableOpacity onPress={() => notCurrentMonth ? {} : this.selectDate(day)}
        style={[styles.warpDay, warpDayStyle,
        dateSelected ? { dateSelectedWarpDayStyle, backgroundColor: '#F4F6F7' } : {}]}
      >
        <View>
          <Text style={[styles.day, textDayStyle,
          isCurrent ? { color: '#E90001', ...currentDayStyle } : {},
          notCurrentMonth ? { color: '#493D40', ...notCurrentDayOfMonthStyle } : {}]}>
            {day.split('-')[2]}
          </Text>
          {renderChildDay(day)}
        </View>
      </TouchableOpacity>
    )
  }

  selectDate(date) {
    if (this.isDateEnable(date)) {
      this.props.selectDate(date)
    }
  }

  yearMonthChange(type, unit, date) {
    let { viewMode, currentYear } = this.state
    if (viewMode === 'day') {
      this.props.calendarChange(type, unit)
    } else {
      this.setState({
        currentYear: currentYear + (type < 0 ? -12 : 12)
      })
    }
    this.props.tabCutoffDate({type, unit, date})
  }

  isDateEnable(date) {
    const { minDate, maxDate } = this.props
    return date >= minDate && date <= maxDate
  }

  render() {
    const {
      calendarMonth, renderPrevYearButton, renderPrevMonthButton,
      renderNextYearButton, renderNextMonthButton,
      weekdayStyle, customWeekdays, warpRowWeekdays,
      warpRowControlMonthYear
    } = this.props
    const weekdays = customWeekdays || ['Sun', 'Mon', 'Tus', 'Wes', 'Thu', 'Fri', 'Sat']
    const data = CommonFn.calendarArray(calendarMonth)
    let dayOfWeek = []
    _.forEach(weekdays, element => {
      dayOfWeek.push(<Text key={element} style={[styles.weekdays, weekdayStyle]}>{element}</Text>)
    })

    return (
      <View style={styles.container}>
        <View style={[{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }, warpRowControlMonthYear]}>
          <TouchableOpacity onPress={() => this.yearMonthChange(-1, 'year', calendarMonth)}>
            {renderPrevYearButton ? renderPrevYearButton() : <MCIcons style={styles.marginH} name='chevron-double-left' size={30} color='#CDD0D4' />}
          </TouchableOpacity>
          <TouchableOpacity onPress={() => this.yearMonthChange(-1, 'month', calendarMonth)}>
            {renderPrevMonthButton ? renderPrevMonthButton() : <MCIcons style={styles.marginH} name='chevron-left' size={30} color='#CDD0D4' />}
          </TouchableOpacity>
          <Text style={styles.txtHeaderDate}>{calendarMonth.split('-')[0]}年</Text>
          <Text style={styles.txtHeaderDate}>{calendarMonth.split('-')[1]}月</Text>
          <TouchableOpacity onPress={() => {
              this.yearMonthChange(1, 'month', calendarMonth)
            }}>
            {renderNextYearButton ? renderNextYearButton() : <MCIcons style={styles.marginH} name='chevron-right' size={30} color='#CDD0D4' />}
          </TouchableOpacity>
          <TouchableOpacity onPress={() => this.yearMonthChange(1, 'year', calendarMonth)}>
            {renderNextMonthButton ? renderNextMonthButton() : <MCIcons style={styles.marginH} name='chevron-double-right' size={30} color='#CDD0D4' />}
          </TouchableOpacity>
        </View>
        <View style={[{ flexDirection: 'row' }, warpRowWeekdays]}>
          {dayOfWeek}
        </View>
        <FlatList
          data={data}
          keyExtractor={(item) => item}
          renderItem={({ item }) => this.renderDay(item)}
          extraData={this.state}
          numColumns={7}
        />
      </View>
    )
  }
}

const propTypes = {
  customWeekdays: PropTypes.array,
  renderPrevYearButton: PropTypes.func,
  renderPrevMonthButton: PropTypes.func,
  renderNextYearButton: PropTypes.func,
  renderNextMonthButton: PropTypes.func,
  //style
  warpRowControlMonthYear: PropTypes.object,
  warpRowWeekdays: PropTypes.object,
  weekdayStyle: PropTypes.object,
  textDayStyle: PropTypes.object,
  currentDayStyle: PropTypes.object,
  notCurrentDayOfMonthStyle: PropTypes.object,
  warpDayStyle: PropTypes.object,
  dateSelectedWarpDayStyle: PropTypes.object,

}

const defaultProps = {
  customWeekdays: ['Sun', 'Mon', 'Tus', 'Wes', 'Thu', 'Fri', 'Sat']
}

CalendarSelect.propTypes = propTypes
CalendarSelect.defaultProps = defaultProps
export default CalendarSelect;