// Custom accordion component
// --------------------------------------------------
// Props:
// - children: content to display
// - title: title of accordion
// - style: style of accordion
// - titleStyle: style of title
// - contentStyle: style of content
// - expanded: whether accordion is expanded
// - onExpand: function to call when accordion is expanded
// - onCollapse: function to call when accordion is collapsed
//
import React, { useState } from 'react'
import { View, StyleSheet, Text, Image } from 'react-native'
import { MaterialIcons } from '@expo/vector-icons'
import { colors, spacing } from '../theme'

import Separator from './Separator'
import Paper from './Paper'

const Accordion = ({
  children,
  title,
  style,
  titleStyle,
  contentStyle,
  expanded = false,
  onExpand,
  onCollapse,
  image,
  chevronColor,
}) => {
  const [isExpanded, setIsExpanded] = useState(expanded)

  const toggle = () => {
    if (isExpanded) {
      setIsExpanded(false)
      onCollapse && onCollapse()
    } else {
      setIsExpanded(true)
      onExpand && onExpand()
    }
  }

  return (
    <Paper style={[styles.container, style]} onPress={toggle}>
      <View style={styles.titleContainer}>
        <Text style={[styles.title, titleStyle]}>{title}</Text>
        <MaterialIcons
          name={isExpanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'}
          size={24}
          color={
            chevronColor
              ? chevronColor
              : titleStyle && titleStyle.color
              ? titleStyle.color
              : colors.primary
          }
        />
      </View>
      {isExpanded && (
        <View style={[styles.content, contentStyle]}>
          {children}
          {image && (
            <Image
              source={{
                uri: image,
              }}
              style={styles.image}
            />
          )}
        </View>
      )}
    </Paper>
  )
}

const styles = StyleSheet.create({
  container: {
    padding: spacing.s_3,
  },
  titleContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
  },
  title: {
    fontSize: 16,
    fontWeight: 'bold',
    color: colors.primary,
  },
  separator: {
    marginLeft: spacing.s_2,
    marginRight: spacing.s_2,
  },
  content: {
    marginTop: spacing.s_3,
  },
  image: {
    width: '100%',
    height: 200,
    resizeMode: 'contain',
    borderRadius: 8,
    marginTop: spacing.s_3,
  },
})

export default Accordion
