package com.tenonedesign.t1autographdemo

import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.tenonedesign.t1autograph.*
import com.tenonedesign.t1autographdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        /**
         * start up autograph for our T1CanvasView (or its subclass)
         */
        val autograph = T1Autograph(this)
        autograph.autographInView(binding.canvasView)
        autograph.licenseCode = "4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"

        /**
         * optional config
         */
//        autograph.showGuideline = false;
//        autograph.strokeColor = Color.BLUE
//        autograph.strokeWidth = 8f
//        autograph.velocityReduction = 0.10f
//        autograph.showDate = true
//        autograph.dateIncludesTime = true
//        autograph.showHash = true
//        autograph.customHash = "your choice of string"
//        autograph.exportScale = 1.5f
//        autograph.clipSignatureToBounds = true
//        autograph.drawOnlyWithStylus = true


        /**
         * this is where the signature or status is received back from T1Autograph
         */
        autograph.listener = object : T1AutographListener {
            override fun onCompleteWithSignature(a: T1Autograph, s: T1Signature) {
                println("Signature completed.")
                // png data
                binding.outputImage.setImageBitmap(BitmapFactory.decodeByteArray(s.pngData, 0, s.pngData.size))
                // pdf data
                s.pdfData
                // svg string
                s.svgString
                // ISO/IEC 19794-7 formatted xml biometric data string
                s.xmlString
            }
            override fun onLineEndWithSignaturePointCount(a: T1Autograph, count: Int) {
                println("A signature stroke ended with total signature point count of $count.")
            }
            override fun onCancel(a: T1Autograph) {
                println("Signature was cancelled.")
            }
        }




        /**
         * T1Autograph includes a built-in modal fragment capable of collecting a signature for you.
         * This is independent of the code above, and may be easier if you’re just starting out.
         * The modal fragment is opened from modal button event handler below.
         *
         * Please note: if the activity is reloaded, the modal listener will need to be set again
         * so here in onCreate is a decent place to bind listeners.
         *
         * A less verbose, more kotlin-like method of setting a listener is used here.
         */
        val autographModal = T1Autograph(this)
        autographModal.licenseCode = "4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"
        autographModal.setOptionalListeners {
            onCompleteWithSignature { a, s ->
                binding.outputImage.setImageBitmap(BitmapFactory.decodeByteArray(s.pngData, 0, s.pngData.size))
            }
            onCancel { a ->
                println("Signature was cancelled.")
            }
        }




        /**
         * Button event handlers
         */
        binding.modalButton.setOnClickListener {
            autographModal.showModal("I hereby authorize the biogenetic payload scheduled for the April 7 launch.")
        }
        binding.doneButton.setOnClickListener {
            autograph.done()
            autograph.reset()
        }
        binding.clearButton.setOnClickListener {
            autograph.reset()
        }


        println("T1Autograph build number: ${autograph.buildNumber}")
    }
}
