// pages/wiki/index.js
import request from '../../utils/request';

Page({
    data: {
        // Categories
        categories: [],
        activeCategory: 'all', // 'all' or category id

        // Display Data
        displayedList: [],

        // Filter State
        searchQuery: '',

        // Pagination State
        current: 1,
        pageSize: 10,
        isLoading: false,
        hasMore: true,
        scrollTop: 0,

        // Modal State
        showIdentifyModal: false,
        isRefreshing: false
    },

    onLoad() {
        this.fetchCategories();
        this.fetchWikiList(true);
    },

    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({ selected: 3 });
        }
    },

    onTabItemTap() {
        this.setData({ scrollTop: Math.random() * 0.01 });
    },

    onRefresh() {
        this.setData({ isRefreshing: true });
        this.fetchWikiList(true).finally(() => {
            this.setData({ isRefreshing: false });
        });
    },

    // Fetch categories from API
    fetchCategories() {
        request.get('/plant/wiki-class/list').then(res => {
            const list = (res && res.list) || (Array.isArray(res) ? res : []);
            this.setData({ categories: list });
        }).catch(err => {
            console.error('Fetch categories failed', err);
        });
    },

    // Fetch wiki list from API
    fetchWikiList(reset = false) {
        if (this.data.isLoading) return Promise.resolve();
        if (!reset && !this.data.hasMore) return Promise.resolve();

        const current = reset ? 1 : this.data.current;

        this.setData({ isLoading: true });

        // Build params
        const params = {
            current: current,
            pageSize: this.data.pageSize
        };

        // Search query
        if (this.data.searchQuery) {
            params.name = this.data.searchQuery;
        }

        // Category filter
        if (this.data.activeCategory !== 'all') {
            params.classId = [this.data.activeCategory];
        }

        return request.post('/plant/wiki/page', params).then(res => {
            const data = res || {};
            const list = data.list || [];
            const total = data.total || 0;

            // Map API data to display model
            const mappedList = list.map(item => ({
                id: item.id,
                name: item.name,
                latinName: item.latinName || '',
                aliases: item.aliases || '',
                genus: item.genus || '',
                difficulty: item.difficulty || 0,
                isHot: item.isHot === true || item.isHot === 1,
                isFavorited: item.hasStar === 1 || item.hasStar === '1',
                image: (item.imgList && item.imgList.length > 0) ? item.imgList[0].url : '',
                classes: (item.classes || []).map(c => c.name),
                // Pass the full item for detail navigation
                raw: item
            }));

            if (reset) {
                this.setData({
                    displayedList: mappedList,
                    current: 2,
                    hasMore: mappedList.length < total,
                    isLoading: false
                });
            } else {
                // Append using data path for performance
                const updateData = {};
                const currentLen = this.data.displayedList.length;
                mappedList.forEach((item, index) => {
                    updateData[`displayedList[${currentLen + index}]`] = item;
                });
                updateData['current'] = current + 1;
                updateData['hasMore'] = (currentLen + mappedList.length) < total;
                updateData['isLoading'] = false;
                this.setData(updateData);
            }
        }).catch(err => {
            console.error('Fetch wiki list failed', err);
            this.setData({ isLoading: false });
        });
    },

    // Toggle Favorite
    async toggleFavorite(e) {
        const id = e.currentTarget.dataset.id;
        const index = this.data.displayedList.findIndex(i => i.id === id);
        if (index === -1) return;

        const item = this.data.displayedList[index];
        const type = item.isFavorited ? 2 : 1;

        try {
            // Attempting consistent API pattern
            await request.get('/plant/wiki/star', { id, type });

            const key = `displayedList[${index}].isFavorited`;
            this.setData({
                [key]: !item.isFavorited
            });

            wx.showToast({ title: type === 1 ? '已收藏' : '已取消', icon: 'success' });
        } catch (err) {
            console.error('Toggle favorite failed', err);
            wx.showToast({ title: '操作失败', icon: 'none' });
        }
    },

    updateItemFavoriteStatus(id, isFavorited) {
        const index = this.data.displayedList.findIndex(i => i.id == id);
        if (index === -1) return;

        const key = `displayedList[${index}].isFavorited`;
        this.setData({
            [key]: isFavorited
        });
    },

    // Search Input Handler (debounced)
    onSearchInput(e) {
        const value = e.detail.value;
        this.setData({ searchQuery: value });

        // Debounce search
        if (this._searchTimer) clearTimeout(this._searchTimer);
        this._searchTimer = setTimeout(() => {
            this.fetchWikiList(true);
        }, 500);
    },

    // Category Filter Handler
    setCategory(e) {
        const catId = e.currentTarget.dataset.cat;
        this.setData({ activeCategory: catId }, () => {
            this.fetchWikiList(true);
        });
    },

    // Infinite Scroll Handler
    onReachBottom() {
        this.fetchWikiList(false);
    },

    // Pull down refresh
    onPullDownRefresh() {
        this.fetchCategories();
        this.fetchWikiList(true);
        wx.stopPullDownRefresh();
    },

    goToDetail(e) {
        const item = e.currentTarget.dataset.item;

        wx.navigateTo({
            url: `/pages/wiki/detail/index?id=${item.id}`,
            success: (res) => {
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: item.raw });
            }
        });
    },

    // Difficulty label helper
    getDifficultyLabel(level) {
        const labels = { 1: '简单', 2: '中等', 3: '较难', 4: '困难', 5: '专家' };
        return labels[level] || '未知';
    },

    openIdentifyModal() { this.setData({ showIdentifyModal: true }); },

    goToAiChat() {
        wx.navigateTo({ url: '/pages/wiki/chat/index' });
    },

    onPopupVisibleChange(e) {
        this.setData({
            showIdentifyModal: e.detail.visible
        });
    },

    closeIdentifyModal() { this.setData({ showIdentifyModal: false }); },

    // Handle plant identification: camera or album
    handleIdentify(e) {
        const source = e.currentTarget.dataset.source; // 'camera' or 'album'

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: [source],
            camera: 'back',
            success: (res) => {
                const tempFilePath = res.tempFiles[0].tempFilePath;

                // Close popup
                this.setData({ showIdentifyModal: false });

                // Store image path in global data for the results page
                const app = getApp();
                app.globalData._identifyImagePath = tempFilePath;

                // Navigate to identify results page
                wx.navigateTo({
                    url: '/pages/wiki/identify/index'
                });
            },
            fail: () => {
                // User cancelled, do nothing
            }
        });
    }
})
