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32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import React, { useEffect, useState, useCallback, useContext, useRef, useMemo } from 'react';
import { Link, router, useLocalSearchParams, useFocusEffect } from 'expo-router';
import { Image as RNImage, StyleSheet, useWindowDimensions, ScrollView, Pressable, RefreshControl, Platform, FlatList, TouchableOpacity } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Icon, MD3Colors, Button, Text, TextInput, TouchableRipple } from 'react-native-paper';
import CircularProgress from 'react-native-circular-progress-indicator';
import { ActivityIndicator } from 'react-native-paper';
import { FlashList } from "@shopify/flash-list";


import uuid from 'react-native-uuid';
import Toast from 'react-native-toast-message';
import { View, AnimatePresence } from 'moti';
import * as Clipboard from 'expo-clipboard';
import * as FileSystem from 'expo-file-system';
import NetInfo from "@react-native-community/netinfo";
import { Marquee } from '@animatereactnative/marquee';
import { Slider } from '@rneui/themed-edge';

import Storage from '@/constants/module/storage';
import ChapterStorage from '@/constants/module/chapter_storage';
import Image from '@/components/Image';
import {CONTEXT} from '@/constants/module/context';
import {blobToBase64, base64ToBlob, getImageLayout} from "@/constants/module/file_manager";
import Theme from '@/constants/theme';
import ChapterImage from '../../components/chapter_image';
import Menu from '../../components/menu/menu';
import Disqus from '../../components/disqus';
import { get_chapter } from '../../modules/get_chapter';
import ComicStorage from '@/constants/module/comic_storage';

const Index = ({}:any) => {
    const SOURCE = useLocalSearchParams().source;
    const COMIC_ID = useLocalSearchParams().comic_id;
    const Dimensions = useWindowDimensions();
    const StaticDimensions = useMemo(() => Dimensions, [])
    

    const {showMenuContext, setShowMenuContext}:any = useContext(CONTEXT)
    const {themeTypeContext, setThemeTypeContext}:any = useContext(CONTEXT)
    const {showCloudflareTurnstileContext, setShowCloudflareTurnstileContext}:any = useContext(CONTEXT)
    const {apiBaseContext, setApiBaseContext}:any = useContext(CONTEXT)

    const [firstLoading, setFirstLoading]:any = useState(true)
    const [chapterInfo, setChapterInfo]:any = useState({})
    const [showOptions, setShowOptions]:any = useState({type:"general",state:false})
    const [imageKeys, setImageKeys]:any = useState([])
    const [isLoading, setIsLoading]:any = useState("")
    const [addChapter, setAddChapter]:any = useState(false)
    const [zoom, setZoom]:any = useState(0)

    const is_adding:any = useRef({state:false,interval:null})
    const temp_loading:any = useRef(false)
    const temp_image_keys:any = useRef({})
    const images:any = useRef({})
    const CHAPTER_IDX = useRef(Number(useLocalSearchParams().idx as string));


    useEffect(()=>{
        setShowMenuContext(false)
    },[])

    useEffect(()=>{(async () => {
        temp_loading.current = true
        const stored_chapter = await ChapterStorage.getByIdx(`${SOURCE}-${COMIC_ID}`,CHAPTER_IDX.current, {exclude_fields:["data"]})   
        setChapterInfo({
            chapter_id: stored_chapter?.id,
            chapter_idx: stored_chapter?.id,
            title: stored_chapter?.title,
        })
        const stored_chapter_zoom = await Storage.get("CHAPTER_ZOOM") || 0
        setZoom(stored_chapter_zoom)
        
        const chapter_current = await get_chapter(SOURCE,COMIC_ID,CHAPTER_IDX.current)
        if (chapter_current){
            images.current = {...images.current,...chapter_current?.files}
            setImageKeys(chapter_current?.file_keys)
            setFirstLoading(false)
        }

        const chapter_next = await get_chapter(SOURCE,COMIC_ID,CHAPTER_IDX.current+1)
        if (chapter_next){
            temp_image_keys.current[`${CHAPTER_IDX.current+1}`] = chapter_next?.file_keys
            images.current = {...images.current,...chapter_next?.files}
        }
        
        temp_loading.current = false
    })()},[])


    useFocusEffect(useCallback(() => {
        return () => {
            setImageKeys([])
            images.current = {}
            clearInterval(is_adding.current.interval)
        }
    },[]))

    useEffect(()=>{
        if (!addChapter) return
        if (is_adding.current.state) return
        is_adding.current.state = true
        setIsLoading(true)
        clearInterval(is_adding.current.interval)
        is_adding.current.interval = setInterval(()=>{
            if (!temp_loading.current) {
                clearInterval(is_adding.current.interval);
                temp_loading.current = true;
                (async ()=>{
                    const new_chapter_idx = CHAPTER_IDX.current+1
                    if (imageKeys.slice(-1)[0]?.type !== "no-chapter-banner"){
                        const next_stored_chapter = await ChapterStorage.getByIdx(`${SOURCE}-${COMIC_ID}`,new_chapter_idx, {exclude_fields:["data"]})   
                        if (next_stored_chapter.data_state === "completed"){
                            if (temp_image_keys.current.hasOwnProperty(new_chapter_idx)){
                                delete temp_image_keys.current[new_chapter_idx-2]
                                const pre_image_keys = imageKeys.filter((data:any) => {
                                    if (data.type === "image"){
                                        const idx = Number(data.value.split("-")[0])
                                        if (idx < new_chapter_idx-2) return false
                                        else return true
                                    }else if (data.type === "chapter-info-banner"){
                                        if (data.idx < new_chapter_idx-2) return false
                                    }else return true
                                })

                                setImageKeys([...pre_image_keys,{type:"chapter-info-banner", idx:CHAPTER_IDX.current, value:{last:chapterInfo.title, next:next_stored_chapter?.title}},...temp_image_keys.current[new_chapter_idx]])
                                setIsLoading(false)
                                setAddChapter(false)
                                is_adding.current.state = false

                                for (const item of Object.keys(images.current)){

                                    if (Number(item.split("-")[0]) < new_chapter_idx-2) delete images.current[item]
                                }
                                const chapter_next = await get_chapter(SOURCE,COMIC_ID,new_chapter_idx + 1)
                                if (chapter_next){
                                    temp_image_keys.current[`${new_chapter_idx + 1}`] = chapter_next?.file_keys
                                    images.current = {...images.current,...chapter_next?.files}
                                }else{
                                    temp_image_keys.current[`${new_chapter_idx + 1}`] = [{type:"no-chapter-banner"}]
                                }
                            }else setImageKeys([...imageKeys,{type:"no-chapter-banner"}])
                        }else setImageKeys([...imageKeys,{type:"no-chapter-banner"}])
                    }
                    setAddChapter(false)
                    setIsLoading(false)
                    temp_loading.current = false
                    is_adding.current.state = false
                })()
            }
        },100)
    },[imageKeys, addChapter])


    return (<>{!firstLoading
        ? <>
            <View
                
                style={{
                    width:"100%",
                    height:"100%",
                    backgroundColor:Theme[themeTypeContext].background_color,
                    zIndex:0
                }}
            >   
                <FlashList 
                    data={imageKeys}
                    renderItem={({item,index}:any) => {
                        if (item.type === "image"){
                            item.image_data = images.current[item.value].data
                            item.layout = images.current[item.value].layout
                        }
                        return <ChapterImage key={index} item={item} zoom={zoom} showOptions={showOptions} setShowOptions={setShowOptions}/>
                    }}
                    estimatedItemSize={StaticDimensions.height}
                    extraData={{zoom:zoom,showOptions:showOptions,setShowOptions:setShowOptions}}
                    onEndReachedThreshold={0.5}
                    onEndReached={()=>{
                        setAddChapter(true)
                    }}
                    onViewableItemsChanged={async ({ viewableItems, changed }) => {
                        const expect_chapter_idx = [CHAPTER_IDX.current + 1, CHAPTER_IDX.current - 1]
                        const current_count = viewableItems.filter((data:any) => data.item.idx === CHAPTER_IDX.current).length
                        const existed_count = viewableItems.filter((data:any) => expect_chapter_idx.includes(data.item.idx)).length
                        
                        if (current_count || existed_count){
                            const choose_idx = current_count > existed_count ? CHAPTER_IDX.current : viewableItems.find((data:any) => expect_chapter_idx.includes(data.item.idx))?.item.idx
                            if (choose_idx === CHAPTER_IDX.current) return
                            const stored_chapter = await ChapterStorage.getByIdx(`${SOURCE}-${COMIC_ID}`,choose_idx, {exclude_fields:["data"]})   
                            setChapterInfo({
                                chapter_id: stored_chapter?.id,
                                chapter_idx: stored_chapter?.id,
                                title: stored_chapter?.title,
                            })
                            const stored_comic = await ComicStorage.getByID(SOURCE, COMIC_ID)
                            if (stored_comic.history.idx && choose_idx > stored_comic.history.idx) {
                                await ComicStorage.updateHistory(SOURCE, COMIC_ID, {idx:stored_chapter?.idx,id:stored_chapter?.id,title:stored_chapter?.title})
                            }
                            router.setParams({idx:choose_idx})
                            CHAPTER_IDX.current = choose_idx
                        }
                        
                        
                    }}
                />
                {isLoading && (
                    <View
                        style={{
                            display:"flex",
                            width:"100%",
                            height:"auto",
                            justifyContent:"center",
                            padding:16,
                        }}
                    >
                        <ActivityIndicator animating={true}  size={(0.03 * ((Dimensions.width+Dimensions.height)/2)) * (1 - zoom/100)}/>
                    </View>
                )}
                
            </View>
            <AnimatePresence exitBeforeEnter>
                {showOptions.state && 
                    <View
                        style={{
                            position:"absolute",
                            width:"100%",
                            height:"100%",
                            display:"flex",
                            flexDirection:"column",
                            zIndex:1,
                        }}
                    >
                        <>{showOptions.type === "general" && <>
                            <Pressable
                                onPress={()=>{setShowOptions({...showOptions,state:!showOptions.state})}}
                                style={{flex:1,backgroundColor:"rgba(0,0,0,0.5)",pointerEvents:"auto"}}
                            >
                                <View  
                                    id='reading-options'
                                    style={{
                                        flex:1,
                                        display:"flex",
                                        flexDirection:"column",
                                        alignItems:"flex-start",
                                        backgroundColor:"transparent",
                                        pointerEvents:"auto",
                                    }}
                                    from={{
                                        opacity: 0,
                                    }}
                                    animate={{
                                        opacity: 1,

                                    }}
                                    exit={{
                                        opacity: 0,
                                    }}
                                    transition={{
                                        type: 'timing',
                                        duration: 500,
                                    }}
                                    exitTransition={{
                                        type: 'timing',
                                        duration: 250,
                                    }}
                                >
                                    
                                        <View 
                                            style={{
                                                width:"100%",
                                                height:"auto",
                                                display:"flex",
                                                flexDirection:"row",
                                                justifyContent:"space-between",
                                                alignItems:"center",
                                                padding:16,
                                                gap:8,
                                                backgroundColor:Theme[themeTypeContext].background_color,
                                                borderBottomWidth:2,
                                                borderColor:Theme[themeTypeContext].border_color,
                                                pointerEvents:"auto",
                                            }}
                                        >
                                            <TouchableRipple
                                                rippleColor={Theme[themeTypeContext].ripple_color_outlined}
                                                style={{
                                                    borderRadius:5,
                                                    borderWidth:0,
                                                    backgroundColor: "transparent",
                                                    padding:5,
                                                    
                                                }}
                                                
                                                onPress={()=>{
                                                    router.replace(`/view/${SOURCE}/${COMIC_ID}/`)
                                                }}
                                            >
                                                <Icon source={"arrow-left-thin"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                            </TouchableRipple>
                                            <View
                                                style={{
                                                    flex:1,
                                                }}
                                            >
                                                <Text selectable={false}
                                                    numberOfLines={1}
                                                    style={{
                                                        color:Theme[themeTypeContext].text_color,
                                                        fontSize:((Dimensions.width+Dimensions.height)/2)*0.045,
                                                        fontFamily:"roboto-bold",
                                                    }}
                                                >
                                                    {chapterInfo.title}
                                                </Text>
                                            </View>
                                            <TouchableRipple
                                                rippleColor={Theme[themeTypeContext].ripple_color_outlined}
                                                style={{
                                                    borderRadius:5,
                                                    borderWidth:0,
                                                    backgroundColor: "transparent",
                                                    padding:5,
                                                }}
                                                
                                                onPress={()=>{
                                                    console.log("HO2h2")
                                                }}
                                            >
                                                <Icon source={"cloud-refresh"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                            </TouchableRipple>
                                        </View>
                                        <View 
                                            style={{
                                                display:"flex",
                                                flexDirection:"column",
                                                width:"100%",
                                                flex:1,
                                                padding:16,
                                                alignItems:"flex-end",
                                                gap:12,
                                                justifyContent:"center",
                                            }}
                                        >
                                            <View style={{width:"auto",height:"auto",display:"flex",flexDirection:"column",alignItems:"center",gap:8, pointerEvents:"auto"}}>
                                                <View style={{
                                                    width:"auto",
                                                    height:"auto",
                                                    backgroundColor: 'rgba(0, 0, 0, 0.5)',
                                                    paddingHorizontal:12,
                                                    paddingVertical:12,
                                                    borderRadius:12,
                                                    
                                                }}>
                                                    <Slider
                                                        style={{ 
                                                            width: ((Dimensions.width+Dimensions.height)/2)*0.06,
                                                            height: Dimensions.height*0.525,
                                                            pointerEvents:"auto",
                                                        }}
                                                        value={zoom}
                                                        onValueChange={async (value)=>{
                                                            await Storage.store("CHAPTER_ZOOM", value)
                                                            setZoom(value)
                                                        }}
                                                        
                                                        maximumValue={80}
                                                        minimumValue={-80}
                                                        step={1}
                                                        
                                                        orientation="vertical"
                                                        
                                                        minimumTrackTintColor={Theme[themeTypeContext].text_color}
                                                        maximumTrackTintColor={Theme[themeTypeContext].border_color}

                                                        thumbStyle={{ 
                                                            height: "auto", 
                                                            width: "auto", 
                                                            backgroundColor: Theme[themeTypeContext].background_color,
                                                            padding:5,
                                                            
                                                        }}
                                                        thumbProps={{children: (<>
                                                            <>{zoom < 0
                                                                && <Icon source={"magnify-plus-outline"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                                            }</>
                                                            <>{zoom === 0
                                                                && <Icon source={"magnify"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                                            }</>
                                                            <>{zoom > 0
                                                                && <Icon source={"magnify-minus-outline"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                                            }</>
                                                        </>)}}

                                                        
                                                    />
                                                    
                                                </View>
                                                <TouchableRipple
                                                    rippleColor={Theme[themeTypeContext].ripple_color_outlined}
                                                    style={{
                                                        borderRadius:5,
                                                        borderWidth:0,
                                                        backgroundColor: "transparent",
                                                        padding:5,
                                                        pointerEvents:"auto",
                                                    }}
                                                    
                                                    onPress={async ()=>{
                                                        await Storage.store("CHAPTER_ZOOM", 0)
                                                        setZoom(0)
                                                    }}
                                                >
                                                    <Icon source={"magnify-remove-cursor"} size={((Dimensions.width+Dimensions.height)/2)*0.05} color={Theme[themeTypeContext].icon_color}/>
                                                </TouchableRipple>
                                            </View>
                                        </View>
                                    
                                    
                                </View>
                                
                            </Pressable>
                        </>}</>
                        <>{showOptions.type === "comments"&& <>
                            <View
                                style={{
                                    width:"100%",
                                    height:"auto",
                                    padding:16,
                                    backgroundColor:Theme[themeTypeContext].background_color,
                                    borderBottomWidth:2,
                                    borderColor:Theme[themeTypeContext].border_color,
                                }}
                            >
                                <Text selectable={false}
                                    numberOfLines={1}
                                    style={{
                                        color:Theme[themeTypeContext].text_color,
                                        fontSize:((Dimensions.width+Dimensions.height)/2)*0.045,
                                        fontFamily:"roboto-bold",
                                    }}
                                >
                                    {chapterInfo.title}
                                </Text>
                            </View>
                            <View
                                style={{
                                    flex:1,
                                    backgroundColor:Theme[themeTypeContext].background_color,
                                }}
                            >   
                                <Disqus title={chapterInfo.title} identifier={`${SOURCE}-${COMIC_ID}-${CHAPTER_IDX.current}`} url={`${apiBaseContext}/read/${SOURCE}/${COMIC_ID}/`}
                                    paddingVertical={16} paddingHorizontal={25}
                                />
                            </View>
                        </>}</>
                        <Menu showOptions={showOptions} setShowOptions={setShowOptions}/>
                    </View>
                }
            </AnimatePresence>
            
        </>
        

        :<View style={{zIndex:5,width:"100%",height:"100%",display:"flex",justifyContent:"center",alignItems:"center",backgroundColor:Theme[themeTypeContext].background_color}}>
            <Image setShowCloudflareTurnstile={setShowCloudflareTurnstileContext} source={require("@/assets/gif/cat-loading.gif")} style={{width:((Dimensions.width+Dimensions.height)/2)*0.15,height:((Dimensions.width+Dimensions.height)/2)*0.15}}/>
        </View>
    }</>)
    

}

export default Index;