/**
 * 地图底部充电站信息组件
 * @邠心vbe on 2022/12/23
 */
import React from 'react';
import { Pressable, StyleSheet, View, Text } from 'react-native';
import { ElevationObject } from '../../../components/Button';
import utils from '../../../utils/utils';
import { ChargeStyle } from '../../charge/Charging';
import { PageList } from '../../Router';
import ConnectType from '../../search/ConnectType';
import app from '../../../../app.json';

export default BottomSiteInfo = ({stationInfo = {}, onFavorite}) => {
  const getAvailable = (type) => {
    const all = stationInfo.allConnector;
    if (all) {
      if (type == 'box') {
        return all.boxAvailable + '/' + all.boxAll;
      } else {
        return all.available + '/' + all.all;
      }
    } else {
      return '0/0';
    }
  }

  const getOperatingHours = () => {
    if (stationInfo.endlessService) {
      return "24/7";
    } else if (stationInfo.operatingHours) {
      return stationInfo.operatingHours;
    } else {
      return $t('charging.toBeUpdated');
    }
  }

  const getParkingFee = () => {
    if (stationInfo.parkingFeeFree) {
      return $t('charging.free');
    } else if (stationInfo.parkingFee) {
      return stationInfo.parkingFee;
    } else {
      return $t('charging.toBeUpdated');
    }
  }

  const toChargePage = () => {
    startPage(PageList.chargeDetailPage, {stationInfo: stationInfo, action: 'view', from: PageList.home})
  }

  if (stationInfo.id) {
    return (
      <View style={styles.stationBarView}>
        <View style={ui.flexc}>
          <Pressable
            style={styles.stationInfo}
            //onPress={() => startPage(PageList.chargeDetail, {stationInfo: stationInfo, action: 'view'})}}
            onPress={toChargePage}>
            <Text
              ellipsizeMode='tail'
              numberOfLines={1}
              style={styles.stationTitle}>{stationInfo.name}</Text>
            <Text
              style={styles.stationAddress}
              ellipsizeMode='tail'
              numberOfLines={2}>{stationInfo.address}</Text>
          </Pressable>
          {/* <View style={styles.stationAvailable}>
            <Image
              style={styles.availableIcon}
              source={require('../../../images/charge/icon-type-stops.png')}/>
            <Text style={styles.availableText}>{getAvailable('box')}</Text>
          </View>
          <View style={styles.stationAvailable}>
            <Image
              style={styles.availableIcon}
              source={require('../../../images/charge/icon-type-interfaces.png')}/>
            <Text style={styles.availableText}>{getAvailable('inc')}</Text>
          </View> */}
          { app.modules.bookmarks &&
            <Pressable
              style={[styles.directIconView, {backgroundColor: stationInfo.favorite ? colorPrimary : colorCancel}]}
              android_ripple={rippleLess}
              onPress={onFavorite}>
              <MaterialIcons
                name="star"
                size={26}
                color={colorLight}/>
            </Pressable>
          }
          <Pressable
            style={styles.directIconView}
            android_ripple={rippleLess}
            onPress={() => {
              utils.directMaps(stationInfo.latitude, stationInfo.longitude, stationInfo.address);
            }}>
            <MaterialCommunityIcons
              name="navigation-variant"
              size={25}
              color={colorLight}/>
            {/* <MaterialIcons
              name='directions'
              size={28}
              color={colorAccent}/>
            <Text style={styles.distanceText}>{stationInfo.distance}</Text> */}
          </Pressable>
        </View>
        <View style={ui.flex}>
          <View style={styles.connectView}>
            <ConnectType {...stationInfo?.acConnector}/>
            <ConnectType {...stationInfo?.dcConnector}/>
          </View>
          <View style={styles.stationStatusView}>
            { (stationInfo.allConnector && stationInfo.allConnector.available > 0) &&
              <Text style={[ChargeStyle.infoStatus, ChargeStyle.statusAvailable, styles.stationStatus]}>{$t("charging.statusAvailable")}</Text>
            }
            <Text style={[ChargeStyle.infoStatus, stationInfo.siteType == 'Public' ? ChargeStyle.statusAvailable : ChargeStyle.statusWarning, styles.siteTypes]}>{stationInfo.siteType}</Text>
          </View>
        </View>
        <View style={styles.divideLine}></View>
        <Pressable
          style={styles.infoDetailsView}
          onPress={toChargePage}>
          <View style={ui.flex1}>
            <Text style={styles.infoTitle}>{$t("charging.operatingHours")}</Text>
            <View style={styles.infoView}>
              <Text style={styles.infoText}>{getOperatingHours()}</Text>
            </View>
          </View>
          <View style={{width: 4}}></View>
          <View style={ui.flex1}>
            <Text style={styles.infoTitle}>{$t("charging.parkingFees")}</Text>
            <View style={styles.infoView}>
              <Text style={styles.infoText}>{getParkingFee()}</Text>
            </View>
          </View>
          <View style={{width: 4}}></View>
          <View style={ui.flex1}>
            <Text style={styles.infoTitle}>{$t("charging.additionalInfo")}</Text>
            <View style={styles.infoView}>
              <Text style={styles.infoText}>{stationInfo?.additionalNotes}</Text>
            </View>
          </View>
        </Pressable>
      </View>
    );
  } else {
    return <></>;
  }
}

const styles = StyleSheet.create({
  stationBarView: {
    left: 16,
    right: 16,
    bottom: 48,
    zIndex: 10,
    borderRadius: 6,
    ...$padding(12, 9),
    position: 'absolute',
    backgroundColor: colorLight,
    ...ElevationObject(3)
  },
  stationInfo: {
    flex: 1,
    height: 45,
    paddingLeft: 4,
    paddingRight: 8,
    justifyContent: 'space-around'
  },
  stationTitle: {
    color: '#000',
    fontSize: 16,
    paddingBottom: 4
  },
  stationAddress: {
    color: '#999',
    fontSize: 12,
  },
  stationAvailable: {
    height: 45,
    paddingLeft: 8,
    paddingRight: 8,
    alignItems: 'center',
    justifyContent: 'space-between'
  },
  stationStatusView: {
    marginRight: 4,
    alignItems: 'center',
    flexDirection: 'row-reverse'
  },
  stationStatus: {
    fontSize: 10,
    ...$padding(2, 6)
  },
  siteTypes: {
    fontSize: 10,
    marginTop: 0,
    marginRight: 4,
    ...$padding(2, 6)
  },
  availableIcon: {
    width: 23,
    height: 23,
  },
  availableText: {
    color: textPrimary,
    fontSize: 13,
    textAlign: 'center'
  },
  directView: {
    zIndex: 1,
    height: 45,
    marginLeft: 8,
    marginRight: 4,
    alignItems: 'center',
    justifyContent: 'space-between'
  },
  distanceText: {
    color: textPrimary,
    fontSize: 12,
  },
  directIconView: {
    zIndex: 1,
    width: 42,
    height: 42,
    marginLeft: 8,
    marginRight: 8,
    borderRadius: 45,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: colorAccent,
    ...ElevationObject(2)
  },
  connectView: {
    flex: 1,
    paddingTop: 12,
    paddingBottom: 12,
    alignItems: 'center',
    flexDirection: 'row'
  },
  divideLine: {
    height: 1,
    backgroundColor: '#AEAEAE'
  },
  infoDetailsView: {
    flexDirection: 'row'
  },
  infoTitle: {
    color: '#000',
    fontSize: 12,
    fontWeight: 'bold',
    ...$padding(8, 0, 8)
  },
  infoView: {
    paddingBottom: 8
  },
  infoText: {
    color: '#444',
    fontSize: 10
  }
})