package com.tenonedesign.t1autographdemojava;

import android.graphics.BitmapFactory;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.tenonedesign.t1autograph.*;
import com.tenonedesign.t1autographdemojava.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        /*
         * start up autograph for our T1CanvasView (or its subclass)
         */
        final T1Autograph autograph = new T1Autograph(this);
        autograph.autographInView(binding.canvasView);
        autograph.setLicenseCode("c32c226070dee1b3ae3461880a91113db154aff0");

        /*
         * optional config
         */
//        autograph.setShowGuideline(false);
//        autograph.setStrokeColor(Color.BLUE);
//        autograph.setStrokeWidth(18f);
//        autograph.setVelocityReduction(0.1f);
//        autograph.setShowDate(true);
//        autograph.setDateIncludesTime(true);
//        autograph.setShowGuideline(false);
//        autograph.setShowHash(true);
//        autograph.setCustomHash("your choice of string");
//        autograph.setExportScale(1.5f);
//        autograph.setClipSignatureToBounds(true);
//        autograph.setDrawOnlyWithStylus(true);



        /*
         * this is where the signature or status is received back from T1Autograph
         */
        autograph.setListener(new T1AutographListener() {

            @Override
            public void onCompleteWithSignature(T1Autograph a, T1Signature s) {
                Log.d("","Signature completed.");
                // png data
                binding.outputImage.setImageBitmap(BitmapFactory.decodeByteArray(s.getPngData(), 0, s.getPngData().length));
                // pdf data
                s.getPdfData();
                // svg string
                s.getSvgString();
                // ISO/IEC 19794-7 formatted xml biometric data string
                s.getXmlString();
            }

            @Override
            public void onLineEndWithSignaturePointCount(T1Autograph a, int i) {
                Log.d("","Line Ended.");
            }

            @Override
            public void onCancel(T1Autograph a) {
                Log.d("","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.
         */
        final T1Autograph autographModal = new T1Autograph(this);
        autographModal.setLicenseCode("c32c226070dee1b3ae3461880a91113db154aff0");
        autographModal.setListener(new T1AutographListener() {
            @Override
            public void onCompleteWithSignature(T1Autograph a, T1Signature s) {
                binding.outputImage.setImageBitmap(BitmapFactory.decodeByteArray(s.getPngData(), 0, s.getPngData().length));
            }
            @Override
            public void onLineEndWithSignaturePointCount(T1Autograph a, int i) {
            }
            @Override
            public void onCancel(T1Autograph a) {
            }
        });


        /*
         * Button event handlers
         */
        binding.modalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autographModal.showModal("I hereby authorize the biogenetic payload scheduled for the April 7 launch.");
            }
        });
        binding.doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autograph.done();
                autograph.reset();
            }
        });
        binding.clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autograph.reset();
            }
        });


        Log.d("","T1Autograph build number: "+autograph.getBuildNumber());

    }
}
