package com.example.beijing_daxing.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;


import com.example.beijing_daxing.R;
import com.google.gson.Gson;
import com.uhf.base.UHFManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.MediaType;

public abstract class BaseActivity extends AppCompatActivity {
    // public static String url = "http://192.168.137.1:8090/api";
    public static String url = "http://119.45.202.115:8090/api";
    public ProgressDialog dialog;
    public Gson gson;
    public MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public UHFManager uhfrManager;
    private MediaPlayer mediaPlayer;

    public Intent broadcastIntent;

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gson = new Gson();
        initDialog();
        initSan();
        broadcastIntent = new Intent("android.rfid.FUN_KEY");
        broadcastIntent.putExtra("keydown", false);
    }


   /* public void sanRFIDIdata() {

        uhfrManager.startInventoryTag();// 开始盘点
        new Thread(new Runnable() { // 开启线程接收标签数据
            @Override
            public void run() {
                String[] buf = uhfrManager.readTagFromBuffer();
                Log.e("TAG", "run:" + buf);
                if (buf == null || buf.length == 0) {
                    return;
                }
                mediaPlayer.start();
                String epc = buf[1];
                Log.e("TAG", "run:" + epc);
                sanRfid(epc);
            }
        }).start();
        uhfrManager.stopInventory();// 停止盘点

    }*/

    public void sanEpc() {

        String[] strings = uhfrManager.readTagFromBuffer();
        if (strings == null || strings.length == 0) {
            return;
        }
        Log.e("TAG", "baseman:" + strings.toString());
        mediaPlayer.start();
        sanRfid(strings[1]);
    }

    private void initDialog() {
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("请求中...");
    }

    private void initSan() {

        uhfrManager = MyApplication.getApplication().getUHFManager();
        // intentFilter.addAction("android.intent.action.FUN_KEY");

        // myReceiver = new MyReceiver();
        // IntentFilter intentFilter = new IntentFilter();
        // intentFilter.addAction("android.rfid.FUN_KEY");
        // registerReceiver(myReceiver, intentFilter);
        mediaPlayer = MediaPlayer.create(this, R.raw.msg);
    }

    public void setPower(int power) {
        boolean b = uhfrManager.powerSet(power);
        if (!b) {
            Toast.makeText(this, "功率修改失败", Toast.LENGTH_SHORT).show();
        }
        Log.e("TAG", "setPower:" + uhfrManager.powerGet());
    }


    public void sanRfid(String epc) {
    }



    public static final int CAMERA_PHOTO = 0X02;
    public File outputImage;
    public Uri img_uri;
    public SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

    // 拍照
    public void takePhoto() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
        outputImage = new File(getExternalCacheDir(), "output_imgage" + format.format(new Date()) + ".jpg");
        try {
            outputImage.createNewFile();
        } catch (IOException e) {
            Log.e("TAG", "teke_photo:文件创建错误");
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= 24) {
            img_uri = FileProvider.getUriForFile(this, "com.example.beijing_daxing", outputImage);
        } else {
            img_uri = Uri.fromFile(outputImage);
        }
        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, img_uri);
            startActivityForResult(intent, CAMERA_PHOTO);
        } else {
            // 提示用户开户权限   拍照和读写sd卡权限
            String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
            ActivityCompat.requestPermissions(this, perms, CAMERA_PHOTO);
        }
    }

    public void takePhotoCall(File outputImage, Bitmap bitmap) {

    }

    // 拍照的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_PHOTO && resultCode == RESULT_OK) {
            Bitmap bitmap = null;
            try {
                Log.e("TAG", "onActivityResult:" + img_uri.getPath());
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(img_uri));
                FileOutputStream outputStream = new FileOutputStream(outputImage);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
                takePhotoCall(outputImage, bitmap);
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}