/**
 * Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
 *
 * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
 * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
 * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
 * This notice may not be removed from this file.
 */

import React, { Component } from 'react';
import DocumentPicker from 'react-native-document-picker'
import {
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  SafeAreaView
} from 'react-native';
import { ComPDFKit, CPDFViewMode } from 'react-native-compdfkit-pdf';
import { CPDFAnnotationType, CPDFConfigTool, CPDFDisplayMode } from '../src/configuration/CPDFOptions';
import { Platform } from 'react-native';


type Props = {
};
export default class App extends Component<Props> {

  state = {
    versionCode: ''
  }

  constructor(props: Props) {
    super(props)

    this.initialize()
    this.getSDKBuildTag()
    this.getVersionCode()
  }

  async getVersionCode() {
    var version = await ComPDFKit.getVersionCode()
    console.log("ComPDFKitRN", "getVersionCode:", version)
    this.setState({
      versionCode: version
    })
  }

  async initialize() {

    var result = await ComPDFKit.initialize('vxwRHJC9RDKK0He2jTpepBkrolOnPQt7bxjfIiqqcJc=', 'vTXxqCm2rutpV67wRy+UnBSJzDBwG0/DLjiLL61zxxc=')
    console.log("ComPDFKitRN", "initialize:", result)
  }

  async init_() {
    var result = await ComPDFKit.init_('77/0LbrlNGNUEKWCmu4/iG3ZlSv4+47zdgsGOxDX3vGx64O2BtvxJyDzgqNvAmqU8eM0G0ALjeYHtV3vrL7mNtev5BHDGmg2ye3WigSqpN8y0gOPPutXKAQyW9vM+cC81ws4sjcXt2vphQXKjRGeVnQodoe+0FbKzffywJ2DORo32GO9qJ51qIKGmXhokKOuQIDJ2eDgQDkIwUChC+yVz088AjTSDSYOe0UobLHOpkP6Ou4qbkx6pKJ+WexOqFxIb90cAQVVa02NpLJdSu8VPIDJNzuwds1y2RLVD6lgBj8Zez+CGDL4JzeYnYhQhlOZNLXJQ4ZqkN/eHcvgIlpzL2u0lH1oY17eVN2TRW5amzqOuoo5orgnAvMGFLdEwMLlC+K5dn2h1bB4RjP9ZTqgoNaGtyKiQ+FhqHLgPV+faNMUdCBlrq4FNafN5ZoZwbHn4fzUh88DO0481O/H5F0zHak/PQJR7Gu1OfN94Q2uALpL4t3i0S76cdEeJ6wRw44AH0PQikF7jWqqmAB1bqcqsgePNE97RigZwYiDA0p2AGWxhBg2+pgZD9EPOOjdtWPXK9LTAop75OQ9whjDWL1y0LTP/JhOPQIOghNPepj3VtjzSVrUbTBFktXeDGlz0NH9TnbvjZoxJRlcNN1+9x100WEfF4A2XbJZEjcpxV9tk1r9UpDZNXkuRC5cqSCOndk3WcAQjbXfQ4Bb9zwxbcvORsTU9lFmAbRS4KFSnCfN/gMqYb0QzhAKt6Wube1sAVkj4n7AvEss/0SdC9zk5m0/E/c0dDshJ3XKSLU/PaI1wbf/SnQhn+gZICJWg9lWCAi16kSStNvD+Tlg8iXYGXcUT967Gjfe/7Au1tVEU3oE60OBrEnSCSSJJt3MWbr/52CRpTtQ6bC+eZK0ijaRGZnS60G4A4sqfUpH3dRQ0juEnz0zrfyaQi4TKGCC1SzT5YPtsIEy4Stbdh3CCWoYV8SehrEkB58JHrolHhy5cVPV2RRYE30JXG5sJOlwb6wuhHVLlanJ7OE5ewEJCtZIHMkJ/rlfinOunS0G9GL2IMBwsyfOB1Cxl+yXx4V3li2ymawe')
    console.log("ComPDFKitRN", "init_:", result)
  }

  async getSDKBuildTag() {
    var buildTag = await ComPDFKit.getSDKBuildTag()
    console.log("ComPDFKitRN", "getSDKBuildTag:", buildTag)
  }

  render() {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.scaffold}>
          <View style={styles.appBar}>
            <Text style={styles.mediumTitle}>
              ComPDFKit PDF SDK for ReactNative
            </Text>
          </View>
          <View style={styles.container}>
            <TouchableOpacity onPress={() => {
              var samplePDF: string = Platform.OS == 'android' ? 'file:///android_asset/pdf_document.pdf' : 'PDF_Document.pdf'
              
              ComPDFKit.openDocument(samplePDF, '', ComPDFKit.getDefaultConfig({
                modeConfig:{
                  initialViewMode: CPDFViewMode.ANNOTATIONS
                }
              }))
            }}>
              <View style={styles.funItem}>
                <Text style={{ fontWeight: 'bold' }}>{'Open Sample'}</Text>
              </View>
            </TouchableOpacity>

            <View style={styles.dividingLine} />

            <TouchableOpacity onPress={() => {
              try {
                const pickerResult = DocumentPicker.pick({
                  type: [DocumentPicker.types.pdf]
                });
                pickerResult.then(res => {
                  console.log('path:', res[0]?.uri)
                  ComPDFKit.openDocument(res[0]?.uri as string, '', ComPDFKit.getDefaultConfig({}))
                })
              } catch (err) {
              }
            }}>
              <View style={styles.funItem}>
                <Text style={{ fontWeight: 'bold' }}>{'Pick Document'}</Text>
              </View>
              <View style={styles.dividingLine} />

            </TouchableOpacity>

            <View style={styles.buttom}>
              <Text style={styles.body2}>ComPDFKit for {Platform.OS == 'android' ? 'Android' : 'iOS'} {this.state.versionCode}</Text>
            </View>
          </View>
        </View>
      </SafeAreaView>

    );
  }
}

const styles = StyleSheet.create({
  appBar: {
    height: 56,
    backgroundColor: '#FAFCFF',
    elevation: 4,
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center",
    padding: 16
  },
  mediumTitle: {
    fontSize: 16,
  },
  body2: {
    textAlign: 'center',
    fontSize: 12
  },
  scaffold: {
    flex: 1,
  },
  container: {
    marginHorizontal: 16,
    marginVertical: 8,
    flex: 1,
    // backgroundColor: '#F5FCFF',
  },
  funItem: {
    height: 56,
    justifyContent: 'center',
    textAlign: 'center'
  },
  dividingLine: {
    height: 0.5, backgroundColor: '#4D333333', width: '100%'
  },
  buttom: {
    flex: 1,
    justifyContent: 'flex-end',
  }
});
