package com.lanyun.wanglaotou.composable

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.google.accompanist.insets.statusBarsHeight
import com.lanyun.common.baseDate.updateMainEvent
import com.lanyun.common.model.MainEvent
import com.lanyun.wanglaotou.KeyNavigationRoute
import com.lanyun.wanglaotou.public.*
import com.lanyun.wanglaotou.viewModel.GuestViewAction
import com.lanyun.wanglaotou.viewModel.GuestViewModel

@Composable
fun GuestCompose(
    navHostController: NavHostController,
    guestViewModel:GuestViewModel = viewModel()
){
    val viewStates = guestViewModel.viewStates
    LcePage(pageState = viewStates.pageState, onRetry = {
        guestViewModel.dispatch(GuestViewAction.FetchData)
    }) {
        BaseScreen {


            AppBar(
                "购买",
                leftIcon= Icons.Default.ArrowBack,
//            rightIcon = Icons.Default.Settings,
                onLeftClick={
                    navHostController.navigateUp()
                }
            )

            SwipeRefreshContent(
                viewModel = guestViewModel,
                listData = viewStates.dataList,
                state = viewStates.listState,
                itemContent = {

                },
                noData = {
                    guestViewModel.dispatch(GuestViewAction.FetchData)
                },
                content = { index,data ->
                    var subPlan = "订阅"
                    viewStates.userSubModel?.let {
                        if (  it.plan_id == data.id){
                            subPlan = "已经订阅"
                        }
                    }

                    PlanItemContent(data.name,data.content, onClick = {
                        // 'redirect': "/plan/${widget.id}
                        navHostController.navigate("${KeyNavigationRoute.WEBVIEW.route}?url=/plan/${data.id}&title=配置订阅")
                    },subPlan=subPlan)
                },
                bottomContent = {

                }
            )
        }
    }




}

@Composable
fun PlanItemContent(
    title : String,
    content:String,
    subPlan: String,
    onClick: () -> Unit = {}
){
    var content_s = ""
    if (content.isNotEmpty()){
        content_s = content.replace("<li>","")
        content_s = content_s.replace("</li>","")
    }
    Row(
        modifier = Modifier
            .clickable(onClick = onClick)
            .fillMaxSize()
            .padding(top = 6.dp, bottom = 6.dp, start = 5.dp, end = 10.dp),

        verticalAlignment = Alignment.CenterVertically
    ) {

        Column() {

            Text(text = title)

            Spacer(modifier = Modifier.height(2.dp))

            Text(text = content_s)

        }
        Spacer(modifier = Modifier.weight(1f,true))
        
        Text(text = subPlan)
        

    }
}