package com.vitorpamplona.amethyst.ui.components

import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat.startActivity
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.lnurl.LnInvoiceUtil

@Composable
fun InvoicePreview(lnInvoice: String) {
  val amount = try {
    LnInvoiceUtil.getAmountInSats(lnInvoice)
  } catch (e: Exception) {
    e.printStackTrace()
    null
  }

  val context = LocalContext.current

  Column(modifier = Modifier
    .fillMaxWidth()
    .padding(start = 30.dp, end = 30.dp)
    .clip(shape = RoundedCornerShape(10.dp))
    .border(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.12f), RoundedCornerShape(15.dp))
  ) {

    Column(
      modifier = Modifier
        .fillMaxWidth()
        .padding(30.dp)
    ) {

      Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
          .fillMaxWidth()
          .padding(bottom = 10.dp)
      ) {
        Icon(
          painter = painterResource(R.drawable.lightning),
          null,
          modifier = Modifier.size(20.dp),
          tint = Color.Unspecified
        )

        Text(
          text = "Lightning Invoice",
          fontSize = 20.sp,
          fontWeight = FontWeight.W500,
          modifier = Modifier.padding(start = 10.dp)
        )
      }

      Divider()

      amount?.let {
        Text(
          text = "${amount.toInt()} sats",
          fontSize = 25.sp,
          fontWeight = FontWeight.W500,
          modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 10.dp),
        )
      } 

      Button(
        modifier = Modifier.fillMaxWidth().padding(vertical = 10.dp),
        onClick = {
          runCatching {
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse("lightning://$lnInvoice"))
            startActivity(context, intent, null)
          }
        },
        shape = RoundedCornerShape(15.dp),
        colors = ButtonDefaults.buttonColors(
          backgroundColor = MaterialTheme.colors.primary
        )
      ) {
        Text(text = "Pay", color = Color.White, fontSize = 20.sp)
      }
    }
  }
}