import RNTest from './react-native-testkit/'
import React from 'react'
import RNFetchBlob from 'react-native-fetch-blob'
import Timer from 'react-timer-mixin'
import firebase from 'firebase'

import {
  StyleSheet,
  Text,
  View,
  ScrollView,
  CameraRoll,
  Platform,
  Dimensions,
  Image,
} from 'react-native';

const fs = RNFetchBlob.fs
const Blob = RNFetchBlob.polyfill.Blob

window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = Blob
window.FormData = RNFetchBlob.polyfill.FormData

const { Assert, Comparer, Info, prop } = RNTest
const describe = RNTest.config({
  group : 'firebase',
  run : true,
  expand : true,
  timeout : 300000000,
})
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
const  dirs = RNFetchBlob.fs.dirs

let prefix = ((Platform.OS === 'android') ? 'file://' : '')
let file = RNTest.prop('image')

// Initialize Firebase
var config = {
  apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
  authDomain: "rnfb-test-app.firebaseapp.com",
  databaseURL: "https://rnfb-test-app.firebaseio.com",
  storageBucket: "rnfb-test-app.appspot.com",
};
firebase.initializeApp(config);

describe('firebase login', (report, done) => {

  firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
    .catch((err) => {
      console.log('firebase sigin failed', err)
    })

  firebase.auth().onAuthStateChanged((user) => {
    report(<Assert key="login status" uid="100"
      expect={true}
      actual={user !== null}/>,
    <Info key="user content" uid="user data">
      <Text>{JSON.stringify(user)}</Text>
    </Info>)
    done()
  })
})

describe('upload file to firebase', (report, done) => {

  // create Blob from BASE64 data
  let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
  let testImage = `firebase-test-${Platform.OS}-${new Date()}.png`
  RNTest.prop('firebase-image', testImage)
  // start test after Blob created
  blob.onCreated(() => {
    let storage = firebase.storage().ref('rnfbtest')
    let task = storage
      .child(RNTest.prop('firebase-image'))
      .put(blob, { contentType : 'image/png' })
      .then((snapshot) => {
        console.log(snapshot.metadata)
        report(<Assert key="upload success"
          expect={true}
          actual={true}/>,
        <Info key="uploaded file stat" >
          <Text>{snapshot.totalBytes}</Text>
          <Text>{JSON.stringify(snapshot.metadata)}</Text>
        </Info>)
        done()
      })
  })
})

describe('download firebase storage item', (report, done) => {
  let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
  storage.getDownloadURL().then((url) => {
    console.log(url)
    report(<Info key="image viewer">
      <Image style={styles.image} source={{uri : url}}/>
    </Info>)
    done()
  })
})

let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`

describe('upload using file path', (report, done) => {
  RNFetchBlob
    .config({ fileCache : true, appendExt : 'jpg' })
    .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
    .then((resp) => {
      report(<Info key="test image">
        <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
      </Info>)
      let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
      blob.onCreated(() => {
        firebase.storage().ref('rnfbtest')
          .child(tier2FileName)
          .put(blob, { contentType : 'image/jpg' })
          .then(() => {
            report(<Assert key="upload finished" />)
            done()
          })
      })
    })
})

describe('verify uploaded file', (report, done) => {
  firebase.storage().ref('rnfbtest/' + tier2FileName)
    .getDownloadURL()
    .then((url) => {
      console.log(url)
      report(<Info key="image viewer">
        <Image style={styles.image} source={{uri : url}}/>
      </Info>)
      done()
    })
})

@@ -3575,32 +3575,54 @@
   %7D)%0A    %7D)%0A%7D)%0A%0A
+let directURL = null%0A%0A
 describe('verify
@@ -3761,32 +3761,31 @@
 %7B%0A      
-console.log(
+directURL = 
 url
-)
 %0A      r
@@ -3782,32 +3782,41 @@
 rl%0A      report(
+%0A        
 %3CInfo key=%22image
@@ -3825,32 +3825,34 @@
 iewer%22%3E%0A        
+  
 %3CImage style=%7Bst
@@ -3884,32 +3884,34 @@
 : url%7D%7D/%3E%0A      
+  
 %3C/Info%3E)%0A      d
@@ -3914,20 +3914,319 @@
    done()%0A    %7D)%0A%7D)%0A
+%0Adescribe('download to base64', (report, done) =%3E %7B%0A  RNFetchBlob.fetch('GET', directURL).then((resp) =%3E %7B%0A    report(%0A      %3CInfo key=%22image data%22%3E%0A        %3CImage%0A          style=%7Bstyles.image%7D%0A          source=%7B%7Buri : 'data:image/jpg;base64 ,'+ resp.base64()%7D%7D/%3E%0A      %3C/Info%3E)%0A    done()%0A  %7D)%0A%7D)%0A

Add test case #44