/* eslint-disable prettier/prettier */
/*This is an Example of Calling Other Class Function in React Native*/
import { Component } from 'react';
import {Alert} from 'react-native';
import moment from 'moment';
import * as RNFS from 'react-native-fs';
const date = moment().utcOffset('+08:00').format('D MMM YYYY');
const time = moment().utcOffset('+08:00').format('h:mm a');
const record = RNFS.DocumentDirectoryPath + '/test.txt';

class OtherClass extends Component {
  functionWithoutArg = () => {
    //function to be called from default class (without args)
    Alert.alert('Function Called Without Argument ');
  };

  functionWithArg = (Value) => {
    //function to called from default class (with args)
    Alert.alert(Value);
  };


  saveRecord = async (correct, wrong, stime) => {
    let obj = {
      correct: correct,
      wrong: wrong,
      timer: stime,
      date: date,
      time: time,
    };
    if (await RNFS.exists(record)) {
      console.log('BLAH EXISTS');
      // write the file
      RNFS.appendFile(
        record,
         'Correct : ' +
          JSON.stringify(obj.correct) +
          ',' +
          'Wrong : ' +
          JSON.stringify(obj.wrong) +
          ',' +
          'Timer : ' +
          stime +
          ',' +
          'Date : ' +
          date +
          ',' +
          'Time : ' +
          time +
          '\n',
        'utf8',
      )
        .then((success) => {
          console.log(JSON.stringify(obj));
        })
        .catch((err) => {
          console.log(err.message);
        });
    } else {
      console.log('BLAH DOES NOT EXIST');
      RNFS.writeFile(
        record,
        'Correct : ' +
          JSON.stringify(obj.correct) +
          ',' +
          'Wrong : ' +
          JSON.stringify(obj.wrong) +
          ',' +
          'Timer : ' +
          stime +
          ',' +
          'Date : ' +
          date +
          ',' +
          'Time : ' +
          time +
          '\n',
        'utf8',
      )
        .then((success) => {
          console.log(JSON.stringify(obj));
        })
        .catch((err) => {
          console.log(err.message);
        });
    }
  };
}

export default OtherClass;
