package com.dmc.callcrm.ui import android.os.Bundle import android.view.Gravity import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import com.dmc.callcrm.R import com.dmc.callcrm.data.CallStats import java.text.SimpleDateFormat import java.util.Locale /** Detail screen for the "Average Call Duration of Connected Calls" Analysis-tab card. */ class AvgDurationDetailActivity : AppCompatActivity() { companion object { const val FROM = "from" const val TO = "to" const val EXCLUDE_DIGITS = "exclude_digits" const val DESELECTED_TYPES = "deselected_types" const val FILTER_LABEL = "filter_label" const val RANGE_LABEL = "range_label" const val EXNO_LABEL = "exno_label" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_avg_duration_detail) val from = intent.getLongExtra(FROM, 0) val to = intent.getLongExtra(TO, System.currentTimeMillis()) val excludeDigits = intent.getStringArrayListExtra(EXCLUDE_DIGITS)?.toSet() ?: emptySet() val deselectedTypes = intent.getStringArrayListExtra(DESELECTED_TYPES)?.toSet() ?: emptySet() findViewById(R.id.btnBack).setOnClickListener { finish() } findViewById(R.id.chipFilter).text = intent.getStringExtra(FILTER_LABEL) ?: "⏷ Filter" findViewById(R.id.chipRange).text = intent.getStringExtra(RANGE_LABEL) ?: "Today ⌄" findViewById(R.id.chipExNo).text = intent.getStringExtra(EXNO_LABEL) ?: "Ex. No.: No ⌄" val df = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()) findViewById(R.id.tvRange).text = "${df.format(from)} – ${df.format(to)}" val r = CallStats(this).avgDurationStats(from, to, excludeDigits, deselectedTypes) findViewById(R.id.tvPerDayBig).text = fmt(r.perDayAvgSec) val perDayRows = findViewById(R.id.rowsPerDay) addKvRow(perDayRows, "No of Days", r.days.toString()) addKvRow(perDayRows, "Total Duration", fmt(r.perDayTotalSec)) addKvRow(perDayRows, "Avg. Duration", fmt(r.perDayAvgSec), bold = true) findViewById(R.id.tvPerCallBig).text = fmt(r.perCallAvgSec) val perCallRows = findViewById(R.id.rowsPerCall) addDirectionRow(perCallRows, "↙ Incoming", 0xFF22C55E.toInt(), r.incomingCalls, r.incomingDurSec, r.incomingAvgSec) addDirectionRow(perCallRows, "↗ Outgoing", 0xFFF59E0B.toInt(), r.outgoingCalls, r.outgoingDurSec, r.outgoingAvgSec) addDirectionRow(perCallRows, "Total Calls", 0xFFFFFFFF.toInt(), r.totalCalls, r.totalDurSec, r.totalAvgSec, bold = true) } private fun addKvRow(container: LinearLayout, label: String, value: String, bold: Boolean = false) { val row = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL setPadding(0, dp(6), 0, dp(6)) } row.addView(TextView(this).apply { text = label; textSize = 13f; setTextColor(0xFFCBD5E1.toInt()) layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) }) row.addView(TextView(this).apply { text = value; textSize = 13f; setTextColor(0xFFFFFFFF.toInt()); gravity = Gravity.END if (bold) setTypeface(typeface, android.graphics.Typeface.BOLD) }) container.addView(row) } private fun addDirectionRow(container: LinearLayout, label: String, color: Int, calls: Int, dur: Long, avg: Long, bold: Boolean = false) { val row = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL setPadding(0, dp(6), 0, dp(6)) } row.addView(TextView(this).apply { text = label; textSize = 13f; setTextColor(color) layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 2f) if (bold) setTypeface(typeface, android.graphics.Typeface.BOLD) }) row.addView(TextView(this).apply { text = calls.toString(); textSize = 13f; setTextColor(0xFFFFFFFF.toInt()); gravity = Gravity.END layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) }) row.addView(TextView(this).apply { text = fmt(dur); textSize = 13f; setTextColor(0xFFFFFFFF.toInt()); gravity = Gravity.END layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) }) row.addView(TextView(this).apply { text = fmt(avg); textSize = 13f; setTextColor(0xFFFFFFFF.toInt()); gravity = Gravity.END layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) if (bold) setTypeface(typeface, android.graphics.Typeface.BOLD) }) container.addView(row) } private fun fmt(s: Long): String { val h = s / 3600; val m = (s % 3600) / 60; val sec = s % 60 return "%dh %dm %ds".format(h, m, sec) } private fun dp(x: Int) = (x * resources.displayMetrics.density).toInt() }