package com.convenient.android.lib.ui.sample.read.setting

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Divider
import androidx.compose.material.RadioButton
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.convenient.android.lib.ui.sample.read.data.ReaderMode
import com.convenient.android.lib.ui.sample.read.readerView
import com.pdf.base.function.annotationDeleteAll
import kotlinx.coroutines.launch

/**
 * @author: hubowen
 * @date: 2022/9/29
 * @description:
 */
@Composable
fun BottomSettingView() {
    var readerModeRemember by remember { mutableStateOf(ReaderMode.VERTICAL_CONTINUE.mode) }
    var readerSinglePageRemember by remember { mutableStateOf(true) }
    var readerClipRemember by remember { mutableStateOf(true) }
    var readerVisiblePageRemember by remember { mutableStateOf(true) }
    var readerAnnotationDeleteAllRemember by remember { mutableStateOf(false) }

    Column(modifier = Modifier.fillMaxSize()) {
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            RadioButton(modifier = Modifier.width(15.dp), selected = readerModeRemember == ReaderMode.VERTICAL_CONTINUE.mode, onClick = { readerModeRemember = ReaderMode.VERTICAL_CONTINUE.mode })
            Text(
                text = "垂直连续",
                modifier = Modifier
                    .clickable(onClick = { readerModeRemember = ReaderMode.VERTICAL_CONTINUE.mode })
            )
            RadioButton(modifier = Modifier.width(15.dp), selected = readerModeRemember == ReaderMode.VERTICAL_N_CONTINUE.mode, onClick = { readerModeRemember = ReaderMode.VERTICAL_N_CONTINUE.mode })
            Text(
                text = "垂直不连续",
                modifier = Modifier
                    .clickable(onClick = { readerModeRemember = ReaderMode.VERTICAL_N_CONTINUE.mode })
            )
        }
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically) {
            RadioButton(modifier = Modifier.width(15.dp), selected = readerModeRemember == ReaderMode.HORIZONTAL_CONTINUE.mode, onClick = { readerModeRemember = ReaderMode.HORIZONTAL_CONTINUE.mode })
            Text(
                text = "水平连续",
                modifier = Modifier
                    .clickable(onClick = { readerModeRemember = ReaderMode.HORIZONTAL_CONTINUE.mode })
            )
            RadioButton(modifier = Modifier.width(15.dp), selected = readerModeRemember == ReaderMode.HORIZONTAL_N_CONTINUE.mode, onClick = { readerModeRemember = ReaderMode.HORIZONTAL_N_CONTINUE.mode })
            Text(
                text = "水平不连续",
                modifier = Modifier
                    .clickable(onClick = { readerModeRemember = ReaderMode.HORIZONTAL_N_CONTINUE.mode })
            )
        }
        Divider()
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = "单页模式")
            Switch(checked = readerSinglePageRemember, onCheckedChange = {
                readerSinglePageRemember = it
            })
        }
        Divider()
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = "裁剪模式")
            Switch(checked = readerClipRemember, onCheckedChange = {
                readerClipRemember = it
            })
        }
        Divider()
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically) {
            Text(text = "页码显示")
            Switch(checked = readerVisiblePageRemember, onCheckedChange = {
                readerVisiblePageRemember = it
            })
        }
        Divider()
        Row(modifier = Modifier
            .clickable(onClick = {
                //删除所有注释
                readerAnnotationDeleteAllRemember = !readerAnnotationDeleteAllRemember
            })
            .fillMaxWidth()
            .height(50.dp)
            .padding(10.dp, 5.dp, 10.dp, 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically) {
            Text(text = "删除所有注释")
        }
        //界面更改
        readerView?.apply {
            //阅读模式
            when (readerModeRemember) {
                ReaderMode.VERTICAL_CONTINUE.mode -> {
                    isContinueMode = true
                    isVerticalMode = true
                }
                ReaderMode.VERTICAL_N_CONTINUE.mode -> {
                    isContinueMode = false
                    isVerticalMode = true
                }
                ReaderMode.HORIZONTAL_CONTINUE.mode -> {
                    isContinueMode = true
                    isVerticalMode = false
                }
                ReaderMode.HORIZONTAL_N_CONTINUE.mode -> {
                    isContinueMode = false
                    isVerticalMode = false
                }
            }
            //单页模式
            isDoublePageMode = !readerSinglePageRemember
            //裁剪模式
            isCropMode = readerClipRemember
        }
        //删除所有注释
        LaunchedEffect(readerAnnotationDeleteAllRemember) {
            readerView?.apply {
                pdfDocument?.annotationDeleteAll(onSuccess = {
                    launch {
                        //重新加载阅读界面
                        if (it) reloadPages()
                    }
                })
            }
        }
    }
}