/**
 * 新版支付方式选择器
 * @邠心vbe on 2021/04/13
 */
import React, { Component } from 'react';
import { View, Text, StyleSheet, Pressable, ScrollView } from 'react-native';
import BottomModal from '../../components/BottomModal';
import BadgeSelectItem from '../../components/BadgeSelectItem';
import { ChargeStyle } from './Charging';
import { PAYTYPE, PaymentIcon } from '../payment/PaymentConfig';
import TextView from '../../components/TextView';
import apiCharge from '../../api/apiCharge';
import Button from '../../components/Button';
import Dialog from '../../components/Dialog';

export default class PaymentListV2 extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      isSelect: true,
      options: [],
      selectIndex: 0,
      selectOptions: {}
    };
  }

  componentDidMount() {
    this.getPaymentOptions();
    this.setState({
      isSelect: (this.props.isSelect || true)
    })
  }

  componentDidUpdate() {
    if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
      this.setState({
        isSelect: this.props.isSelect
      })
    }
  }

  getPaymentOptions() {
    apiCharge.getPaymentTypeOptions({
      chargeBoxId: this.props.chargeBoxId
    }).then(res => {
      if (res.data) {
        this.setState({
          options: res.data
        })
        if (this.props.payType && this.props.payType.code) {
          for (let i = 0; i < res.data.length; i++) {
            let type = res.data[i];
            if (type.code == this.props.payType.code) {
              this.changeMethod(i);
              break;
            }
          }
        } else {
          let index = 0;
          for (let i = 0; i < res.data.length; i++) {
            let type = res.data[i];
            if (type.def) {
              index = i;
              break;
            }
          }
          this.changeMethod(index);
        }
      }
    }).catch(err => {

    })
  }

  showDialog(visible) {
    if (this.state.isSelect) {
      this.setState({
        visible: visible
      })
    }
  }

  changeMethod(index) {
    const type = this.state.options[index];
    this.setState({
      selectIndex: index,
      selectOptions: type
    }, () => {
      if (this.props.onMethodChange) {
        this.props.onMethodChange(this.state.selectOptions);
      }
    })
  }

  setDefault() {
    const code = this.state.selectOptions?.code;
    if (code && !this.state.selectOptions.def) {
      Dialog.showProgressDialog();
      apiCharge.setDefaultPaymentType({
        defaultPaymentMethod: code
      }).then(res => {
        toastShort("success");
        this.getPaymentOptions();
      }).catch(err => {
        toastShort(err)
      }).finally(() => {
        Dialog.dismissLoading();
      })
    }
  }

  render() {
    return (
      <View>
        <BadgeSelectItem
          style={ChargeStyle.stationInfoView}
          checked={false}
          onPress={() => this.showDialog(true)}>
          <PaymentIcon
            method={"WALLET"}
            checked={true}/>
          <View style={styles.paymentView}>
            <TextView style={styles.valueText}>{this.state.selectOptions?.name}</TextView>
            <TextView style={styles.paymentText}>{this.state.selectOptions?.desc}</TextView>
          </View>
          { this.state.selectOptions?.def &&
            <TextView style={styles.textDefault}>Default</TextView>
          }
          { this.state.isSelect &&
            <FontAwesome6
              name={"angle-right"}
              size={16}
              color={colorCancel}
            />
          }
        </BadgeSelectItem>
        <BottomModal
          visible={this.state.visible}
          onHide={() => this.showDialog(false)}>
          <View style={styles.dialogContent}>
            <TextView style={styles.titleText}>Payment Methods</TextView>
            <ScrollView style={styles.paymentList}>
              { this.state.options.map((item, index) => (
                <Pressable
                  key={index}
                  style={styles.itemPayment}
                  onPress={() => this.changeMethod(index)}>
                    <View style={ui.flex1}>
                      <TextView style={styles.valueText}>{item.name}</TextView>
                      <TextView style={styles.descText}>{item.desc}</TextView>
                    </View>
                    { item.def &&
                      <TextView style={styles.tagDefault}>Default</TextView>
                    }
                    <MaterialCommunityIcons
                      name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
                      size={24}
                      color={index == this.state.selectIndex ? colorPrimary : textPrimary}
                    />
                  </Pressable>
                ))
              }
            </ScrollView>
            <EndView/><EndView/>
            <View style={ui.flexc}>
              <Button 
                style={styles.defButton}
                text={"Set Default"}
                onClick={() => this.setDefault()}/>
              <EndView half/>
              <Button
                style={ui.flex1}
                text={"Confirm"}
                onClick={() => this.showDialog(false)}/>
            </View>
          </View>
        </BottomModal>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  paymentView: {
    flex: 1,
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'space-around'
  },
  dialogContent: {
    padding: 16
  },
  paymentList: {
    height: $vh(50)
  },
  titleText: {
    fontSize: 16,
    fontWeight: 'bold',
    paddingBottom: 18
  },
  valueText: {
    color: textPrimary,
    fontSize: 15,
    fontWeight: 'bold'
  },
  paymentText: {
    color: textSecondary,
    fontSize: 12,
    paddingLeft: 8,
    paddingRight: 16
  },
  descText: {
    color: colorAccent,
    fontSize: 12,
    paddingTop: 2
  },
  itemPayment: {
    paddingTop: 8,
    paddingBottom: 8,
    alignItems: 'center',
    flexDirection: 'row'
  },
  tagDefault: {
    color: textLight,
    fontSize: 12,
    borderRadius: 4,
    ...$padding(4, 8),
    marginRight: 12,
    backgroundColor: colorAccent
  },
  textDefault: {
    color: colorAccent,
    fontSize: 12,
    marginRight: 8,
    fontWeight: 'bold'
  },
  defButton: {
    flex: 1,
    backgroundColor: colorPrimary
  }
})
