package com.test.ocr;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import java.io.IOException;

import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private String result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //aksk auth ocr service
        akskOcrService();
        //token auth ocr service
        //tokenOcrService();
    }

    /**
     * aksk auth ocr service
     */
    public void akskOcrService() {
        // TODO: Set required parameters
        String AK = "xxx";// AK from authentication
        String SK = "xxx";// SK from authentication
        String httpEndPoint = "ocr.cn-north-4.myhuaweicloud.com";
        // initialize HWOcrClientAKSK from ak,sk and endpoint information
        HWOcrClientAKSK hwOcrClientAKSK = new HWOcrClientAKSK(this, AK, SK, httpEndPoint);
        // ocr service
        String uri = "/v1.0/ocr/id-card";
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.idcarddemo);
        TextView resultView = findViewById(R.id.requestUriTextView);
        resultView.setText(uri);
        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);
        hwOcrClientAKSK.requestOcrAkskService(uri, bitmap, null, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                result = e.toString();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView resultView = findViewById(R.id.resultTextView);
                        resultView.setText(result);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                result = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView resultView = findViewById(R.id.resultTextView);
                        resultView.setText(result);
                    }
                });

            }
        });
    }

    /**
     * token auth ocr service
     */
    public void tokenOcrService() {
        // TODO: Set required parameters
        String domainName = "xxx"; // if the user isn't IAM user, domain_name is the same with username
        String userName = "xxx";
        String password = "xxx";
        String endPoint = "ocr.cn-north-4.myhuaweicloud.com";
        String region = "cn-north-4";
        // get ocr service token
        HWOcrClientToken ocrToken = new HWOcrClientToken(this, domainName, userName, password, region, endPoint);
        // ocr service
        String uri = "/v1.0/ocr/id-card";
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.idcarddemo);
        TextView resultView = findViewById(R.id.requestUriTextView);
        resultView.setText(uri);
        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);
        ocrToken.requestOcrTokenService(uri, bitmap, null, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                result = e.toString();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView resultView = findViewById(R.id.resultTextView);
                        resultView.setText(result);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                result = response.body().string();
                Log.i("respose", "result = " + result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView resultView = findViewById(R.id.resultTextView);
                        resultView.setText(result);
                    }
                });
            }
        });
    }

}
