package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.databinding.ActivityMainBinding;

/**
  *  This example is using libsenxor_android.SenXorView as the media to drive SenXorDevice on Android.
  *  SenXorView is a wrapper ImageView class to get rid of annoying process pipeline and massage handling.
  *  User can init SenXor parameters by xml attributes, get controller in main program.
  *  However, the flexibility of sensor control decrease compare to Example2.
  *
  *  Author: Meridian Innovation
  *  SDK Version: v3.2.0
 */

public class MainActivity extends AppCompatActivity
{

    private ActivityMainBinding mActivityMainBinding;
    private MainBroadcastReceiver mMainBroadcastReceiver;
    private IntentFilter mIntentFilter;

    /**
     * onPause
     * Android App activity cycle.
     * @param       None
     * @exception   None
     * @return      None
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //Binding setup
        this.mActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(this.mActivityMainBinding.getRoot());

        // Setup broadcast receiver
        this.mMainBroadcastReceiver = new MainBroadcastReceiver();
        this.mIntentFilter = new IntentFilter();
        this.mIntentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

        // Setup event handler
        SenxorsdkExample1ClickHandler clickListener = new SenxorsdkExample1ClickHandler();
        this.mActivityMainBinding.buttonRun.setOnClickListener(clickListener);
    }

    /**
     * onPause
     * Android App activity cycle.
     * @param       None
     * @exception   None
     * @return      None
     */
    @Override
    protected void onResume()
    {
        super.onResume();

        //Register receiver
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
        {
            registerReceiver(this.mMainBroadcastReceiver, this.mIntentFilter, Context.RECEIVER_EXPORTED);
        }
        else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU)
        {
            registerReceiver(this.mMainBroadcastReceiver, this.mIntentFilter,RECEIVER_EXPORTED);
        }
        else
        {
            registerReceiver(this.mMainBroadcastReceiver, this.mIntentFilter);
        }//End if-else

        // Setup thermal view
        this.mActivityMainBinding.imageViewThermal.setup();

        // Check whether SenXor is attached
        if(!this.mActivityMainBinding.imageViewThermal.getSenXorController().isEmpty())
        {
            this.mActivityMainBinding.imageViewThermal.start();
            this.mActivityMainBinding.imageViewThermal.setVisibility(View.VISIBLE);
            this.mActivityMainBinding.buttonRun.setIconResource(R.drawable.icon_senxor_stop_light);
            this.mActivityMainBinding.buttonRun.setEnabled(true);
            this.mActivityMainBinding.buttonRun.setVisibility(View.VISIBLE);
            this.mActivityMainBinding.buttonRun.setTooltipText(getString(R.string.TT_SENXOR_STOP));
            this.mActivityMainBinding.TVMSG.setVisibility(View.INVISIBLE);
        }
        else
        {
            this.mActivityMainBinding.imageViewThermal.setVisibility(View.INVISIBLE);
            this.mActivityMainBinding.buttonRun.setIconResource(R.drawable.icon_senxor_start_light);
            this.mActivityMainBinding.buttonRun.setVisibility(View.INVISIBLE);
            this.mActivityMainBinding.buttonRun.setTooltipText(getString(R.string.TT_SENXOR_RUN));
            this.mActivityMainBinding.TVMSG.setVisibility(View.VISIBLE);
        }//End if-else

    }

    /**
     * onPause
     * Android App activity cycle.
     * @param       None
     * @exception   None
     * @return      None
     */
    @Override
    protected void onPause()
    {
        super.onPause();
        this.mActivityMainBinding.imageViewThermal.stop();
        this.mActivityMainBinding.buttonRun.setIconResource(R.drawable.icon_senxor_start_light);
    }

    /**
     * SenxorsdkExample1ClickHandler
     * Inner class defined the event handler of button
     */
    private class SenxorsdkExample1ClickHandler implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            if (v.getId() == R.id.button_run)
            {

                    if(mActivityMainBinding.imageViewThermal.getSenXorController().getIsRunning())
                    {
                        mActivityMainBinding.buttonRun.setIconResource(R.drawable.icon_senxor_start_light);
                        mActivityMainBinding.buttonRun.setTooltipText(getString(R.string.TT_SENXOR_RUN));
                        mActivityMainBinding.imageViewThermal.stop();
                    }
                    else
                    {
                        mActivityMainBinding.buttonRun.setIconResource(R.drawable.icon_senxor_stop_light);
                        mActivityMainBinding.buttonRun.setTooltipText(getString(R.string.TT_SENXOR_STOP));
                        mActivityMainBinding.imageViewThermal.start();
                    }// End if-else

            }
        }
    }// End class SenxorsdkExample1

    /**
     * MainBroadcastReceiver
     * Inner class for handling broadcast receiver
     */
    private class MainBroadcastReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            final String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
            {
                mActivityMainBinding.buttonRun.setVisibility(View.INVISIBLE);
                mActivityMainBinding.imageViewThermal.setVisibility(View.INVISIBLE);
                mActivityMainBinding.TVMSG.setVisibility(View.VISIBLE);
            }//End if-else
        }
    }
}
