require "import"
import "android.widget.*"
import "java.io.BufferedWriter"
import "java.io.FileWriter"
import "java.io.IOException"
import "android.content.Context"
import "android.content.Intent"
import "java.util.Locale"
import "android.speech.SpeechRecognizer"
import "android.speech.RecognitionListener"
import "android.speech.RecognizerIntent"

local numberTable = dofile("/storage/emulated/0/解说/Plugins/Voice caller 1.0/Contact.lua")
function hasInternetConnection()
    local connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE)
    local networkInfo = connectivityManager.getActiveNetworkInfo()
    return networkInfo ~= nil and networkInfo.isConnected()
end

function kill()
    speechRecognizer.destroy()
    speechRecognizer = nil
    return true
end

function startListening()
    if not hasInternetConnection() then
        service.speak("இணைய இணைப்பை சரி பார்க்கவும்")
        return false
    end
    
    local verbalizationDetected = false
    local recognizedName = ""
    
    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
    
    speechListener = RecognitionListener{
        onResults = function(results)
            data = results.getParcelableArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
            if data ~= nil and data.size() > 0 then
                verbalizationDetected = true
                recognizedName = data.get(0)
                
                local number = numberTable[recognizedName]
                if number ~= nil then
                    service.callPhone(number)
                    return true
                else
-- தொடக்கம்
layout = {
  LinearLayout;
  orientation = "vertical";
  {
    TextView;
    text = "Save Contact";
    layout_gravity = "center_horizontal";
    textSize = "20sp";
    paddingTop = "20dp";
    paddingBottom = "20dp";
  };
  {
    EditText;
    id = "answer";
    inputType = "text";
    layout_width = "match_parent";
    layout_height = "wrap_content";
  };
  {
    EditText;
    id = "editText";
    inputType = "number";
    layout_width = "match_parent";
    layout_height = "wrap_content";
  };
  {
    Button;
    text = "OK";
    onClick = function()
      local answerText = answer.text
      local trimmedAnswerText = answerText:gsub("^%s*(.-)%s*$", "%1")
      local modifiedText = "[\"" .. trimmedAnswerText .. "\"] = "
      local editTextText = editText.text
      local trimmedEditTextText = editTextText:gsub("^%s*(.-)%s*$", "%1")
      local modifiedEditTextText = "\"" .. trimmedEditTextText .. "\",\n"

      -- Save result to a file
      local filepath = "/storage/emulated/0/解说/Plugins/Voice caller 1.0/Contact.lua"

      local file, error = io.open(filepath, "r")
      if file == nil then
        print("Error opening file: " .. error)
        dlg.dismiss()
        return
      end

      -- Read existing content from the file
      local content = file:read("*a")
      file:close()

      -- Insert the modifiedText and modifiedEditTextText in the second line
      content = content:gsub("\n", "\n" .. modifiedText .. modifiedEditTextText, 1)

      -- Write the modified content back to the file
      file, error = io.open(filepath, "w")
      if file == nil then
        print("Error opening file: " .. error)
        dlg.dismiss()
        return
      end

      file:write(content)
      file:close()

service.speak("தொடர்பு சேமிக்கப்பட்டது")
      dlg.dismiss()
    end,
  };
}

dlg = LuaDialog(service)
dlg.View = loadlayout(layout)
dlg.show()
-- முடிவு
                end
            end
        end
    }
    
    recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ta_IN")
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName())
    
    speechRecognizer.startListening(recognizerIntent)
    speechRecognizer.setRecognitionListener(speechListener)
    
    return true
end

startListening()
return true