package com.wmdigit.wmposdemo.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;


import com.wmdigit.wmpos.WmAceKG;
import com.wmdigit.wmpos.ai.support.AiSupportActivity;
import com.wmdigit.wmpos.bean.ScaleBitmap;
import com.wmdigit.wmpos.bean.ScaleSetting;
import com.wmdigit.wmpos.cam.support.CamSupportActivity;
import com.wmdigit.wmposdemo.R;
import com.wmdigit.wmposdemo.databinding.ActivityCropPicBinding;
import com.wmdigit.wmposdemo.view.CropView;

public class CropPicActivity extends AiSupportActivity implements View.OnClickListener {

    private ActivityCropPicBinding dataBinding;
    TextureView textCameraView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_crop_pic);
//        setContentView(R.layout.activity_crop_pic);
        textCameraView = findViewById(R.id.text_camera_view);
        super.onCreate(savedInstanceState);
       dataBinding.tvSure.setOnClickListener(this);
        dataBinding.tvCancel.setOnClickListener(this);
        dataBinding.tvCrop.setOnClickListener(this);
    }

    @Override
    public TextureView getTextureView() {
        return textCameraView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_crop:
                dataBinding.rlPreview.setVisibility(View.GONE);
                dataBinding.rlCrop.setVisibility(View.VISIBLE);
                final ScaleBitmap clearestScaleBitmap = getScaleBitmap(false);
                Bitmap raw = clearestScaleBitmap.getRaw();
                dataBinding.cropImg.setImageBitmap(raw);
                break;
            case R.id.tv_sure:
                dataBinding.cropImg.cropImage(new CropView.OnCropListener() {
                    @Override
                    public void onCropFinished(final Bitmap bitmap, final int left, final int top, final int width, final int height) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ScaleSetting scaleSetting = new ScaleSetting(left, top, width, height);

                                int code = WmAceKG.saveScaleSetting(scaleSetting);
                                if (code == 0) {
                                    Toast.makeText(CropPicActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(CropPicActivity.this, WeighingActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Toast.makeText(CropPicActivity.this, "设置失敗，错误码：" + code, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }


}
