// MainScreen.kt
package com.example.initiateaphonecallapp.ui

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.List
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import com.example.initiateaphonecallapp.service.PhoneCallService
import com.example.initiateaphonecallapp.ui.calllog.CallLogScreen
import com.example.initiateaphonecallapp.ui.contact.ContactScreen
import com.example.initiateaphonecallapp.ui.home.HomeScreen
import com.example.initiateaphonecallapp.ui.pipeline.PipelineScreen
import com.example.initiateaphonecallapp.ui.profile.ProfileScreen

sealed class BottomNavItem(val route: String, val title: String, val icon: @Composable () -> Unit) {
    object Home : BottomNavItem("home", "系统主页", {
        Icon(Icons.Default.Home, contentDescription = "主页")
    })
    object Pipeline : BottomNavItem("pipeline", "客户管道", {
        Icon(Icons.Default.List, contentDescription = "客户管道")
    })
    object CallLog : BottomNavItem("call_log", "通话记录", {
        Icon(Icons.Default.Call, contentDescription = "通话记录")
    })
    object Contact : BottomNavItem("contact", "联系人", {
        Icon(Icons.Default.Person, contentDescription = "联系人")
    })
    // 在 BottomNavItem sealed class 中添加
    object Profile : BottomNavItem("profile", "个人中心", {
        Icon(Icons.Default.Person, contentDescription = "个人中心")
    })
}

@Composable
fun MainScreen(authToken: String, onLogout: () -> Unit) {
    var currentScreen by rememberSaveable { mutableStateOf(BottomNavItem.Home.route) }
    val context = LocalContext.current

    Scaffold(
        bottomBar = {
            NavigationBar {
                listOf(
                    BottomNavItem.Home,
//                    BottomNavItem.Pipeline,
                    BottomNavItem.CallLog,
                    BottomNavItem.Contact,
                    BottomNavItem.Profile  // 添加个人中心
                ).forEach { item ->
                    NavigationBarItem(
                        icon = { item.icon() },
                        label = { Text(item.title) },
                        selected = currentScreen == item.route,
                        onClick = {
                            currentScreen = item.route
                        }
                    )
                }
            }
        }
    ) { innerPadding ->
        when (currentScreen) {
            BottomNavItem.Home.route -> HomeScreen(
                authToken = authToken,
                onLogout = onLogout,
                modifier = Modifier.padding(innerPadding)
            )
            BottomNavItem.Pipeline.route -> PipelineScreen(
                modifier = Modifier.padding(innerPadding)
            )
            BottomNavItem.CallLog.route -> CallLogScreen(
                modifier = Modifier.padding(innerPadding),
                onCallRecordClick = { record ->
                    // 处理通话记录点击事件
                    println("点击通话记录: ${record.phoneNumber}, 状态: ${record.status}")
                },
                onCallBack = { phoneNumber ->
                    // 处理回拨功能
                    println("回拨号码: $phoneNumber")
                    initiatePhoneCall(context, phoneNumber)
                },
                onPlayAudio = { audioUrl ->
                    // 处理播放录音
                    println("播放录音: $audioUrl")
                    // 这里可以添加播放音频的逻辑
                }
            )
            BottomNavItem.Contact.route -> ContactScreen(
                modifier = Modifier.padding(innerPadding),
                onContactClick = { contact ->
                    // 处理联系人点击事件（查看详情等）
                    println("点击联系人: ${contact.name}, 电话: ${contact.phone}")
                },
                onCallContact = { phoneNumber ->
                    // 使用 PhoneCallService 拨打电话
                    initiatePhoneCall(context, phoneNumber)
                }
            )
// 在 when 语句中添加 Profile 路由
            BottomNavItem.Profile.route -> ProfileScreen(
                modifier = Modifier.padding(innerPadding)
            )
        }
    }
}

/**
 * 启动电话拨号服务
 */
private fun initiatePhoneCall(context: Context, phoneNumber: String) {
    val intent = Intent(context, PhoneCallService::class.java).apply {
        action = "ACTION_INITIATE_CALL"
        putExtra("PHONE_NUMBER", phoneNumber)
    }
    ContextCompat.startForegroundService(context, intent)
}