package com.example.sonya

import android.Manifest
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import java.io.OutputStream
import java.net.Socket

class MainActivity : AppCompatActivity() {

    private val sampleRate = 44100
    private var isRecording = false
    private lateinit var recordButton: Button
    private var animator: ObjectAnimator? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recordButton = findViewById(R.id.recordButton)

        // Запрос разрешений
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.INTERNET), 1)

        recordButton.setOnClickListener {
            if (isRecording) {
                stopRecording()
            } else {
                startRecording()
            }
        }
    }

    @SuppressLint("MissingPermission")
    private fun startRecording() {
        isRecording = true
        animateButton(true) // Начинаем анимацию

        Thread {
            val bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)
            val audioRecord = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize)
            audioRecord.startRecording()

            try {
                val socket = Socket("192.168.0.105", 6543)
                val outputStream: OutputStream = socket.getOutputStream()
                val buffer = ByteArray(bufferSize)

                while (isRecording) {
                    val read = audioRecord.read(buffer, 0, buffer.size)
                    outputStream.write(buffer, 0, read)
                }

                outputStream.close()
                socket.close()
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                audioRecord.stop()
                audioRecord.release()
            }
        }.start()
    }

    private fun stopRecording() {
        isRecording = false
        animateButton(false) // Останавливаем анимацию
    }

    private fun animateButton(isRecording: Boolean) {
        if (isRecording) {
            // Запускаем анимацию
            animator = ObjectAnimator.ofFloat(recordButton, "scaleX", 1.0f, 1.2f)
            animator?.duration = 300
            animator?.repeatCount = ObjectAnimator.INFINITE
            animator?.repeatMode = ObjectAnimator.REVERSE
            animator?.start()
        } else {
            // Останавливаем анимацию
            animator?.cancel()
            recordButton.scaleX = 1.0f // Возвращаем к исходному размеру
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <Button
        android:id="@+id/recordButton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="@drawable/circular_button"
        android:text="Start"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        tools:ignore="HardcodedText" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/holo_red_dark" />
</shape>