import { _decorator, Color, Component, instantiate, Label, Layout, Node, PageView, Prefab, Sprite, SpriteFrame, UITransform } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { tools } from '../../tools';
import { car_lib_list_item } from './car_lib_list_item';
import { car_lib_page_item } from './car_lib_page_item';
import { uiManager } from '../../manager/uiManager';
import { config } from '../../config';
import { GameManager } from '../../GameManager';
import { bag_type, car_type, rewards_item_data } from '../../data';
import { car_info } from '../car_info/car_info';
import { SdkUtil } from '../../sdkUtil';
import { userDataManager } from '../../manager/userDataManager';
import { reward_tips_view } from '../reward_tips_view/reward_tips_view';
const { ccclass, property } = _decorator;

@ccclass('car_lib_bottom')
export class car_lib_bottom extends base_ui {
    @property(Node) btn_num_left:Node = null
    @property(Node) btn_num_right:Node = null
    @property(Node) lab_num:Node = null
    @property(Node) pageView:Node = null
    @property(Node) pageView_content:Node = null
    @property(Prefab) pageView_page_item:Prefab = null

    private num_current_count:number = 1
    private num_total_count:number = 1
    private current_select_list_item:car_lib_list_item = null

    private m_car_suipian_list = null
    private m_click_item_cb = null

    private game_status_parent:Node = null

    start() {
        this.onButtonListen(this.btn_num_left, ()=>{
            if(this.num_current_count==1) { 
                return 
            }
            this.num_current_count -=1
            this.updateNumStatus()
        })
        this.onButtonListen(this.btn_num_right, ()=>{
            if(this.num_current_count>=this.num_total_count) {
                return
            }
            this.num_current_count +=1
            this.updateNumStatus()
        })   
        this.pageView.on(PageView.EventType. PAGE_TURNING, (e:PageView)=>{
            let currentPageIndex = e.getCurrentPageIndex()
            // console.log('currentPageIndex=',currentPageIndex)
            this.num_current_count = currentPageIndex + 1
            this.updateNumStatus()
        }) 
    }

    public init(click_item_cb) {
        this.m_click_item_cb = click_item_cb
        this.requestData()
    }

    private requestData() {
        this.num_total_count = tools.all_car_page_list.length
        this.updateNumStatus()

        this.pageView.getComponent(PageView).removeAllPages()
        let id = bag_type.car_suipian
        GameManager.requestBagList(id, (car_suipian_list)=>{
            this.m_car_suipian_list = car_suipian_list
            // console.log('car_suipian_list=',car_suipian_list)
            for (let index = 0; index < this.num_total_count; index++) {
                let page = instantiate(this.pageView_page_item)
                let data_list = tools.all_car_page_list[index]
                page.getComponent(car_lib_page_item).init(this.pageView, index,data_list, car_suipian_list, this.onSelectedItem.bind(this), this.onClickListItem.bind(this))
                this.pageView.getComponent(PageView).addPage(page)
            }
        })
    }

    private getNumCurrentIndex():number {
        let index = this.num_current_count - 1
        if(index<0) {
            index = 0
        }
        return index
    }

    private updateNumStatus() {
        this.lab_num.getComponent(Label).string = this.num_current_count + '/' + this.num_total_count
        let index = this.getNumCurrentIndex()
        this.pageView.getComponent(PageView).scrollToPage(index)
    }

    private onSelectedItem(page_item:car_lib_page_item, list_item:car_lib_list_item) {
        this.current_select_list_item = list_item
        setTimeout(()=>{
            this.pageView.getComponent(PageView).scrollToPage(page_item.getIndex())
        },100)
    }

    private onClickListItem(page_item:car_lib_page_item,list_item:car_lib_list_item) {
        let car_item_data = list_item.getData()
        if(list_item.getIsJiesuo()==false&&car_item_data.stype==car_type.suipian) {
            let cur_count = car_item_data.temp_bag_list_item_data.quantity
            if(cur_count-car_item_data.unlock_points>=0) {
                // 直接显示兑换车辆视图
                GameManager.showExchangeCarView(car_item_data.id,car_item_data.temp_bag_list_item_data.icon,car_item_data.temp_bag_list_item_data.quantity, ()=>{
                    list_item.setJiesuoSelectedStatus()
                })
                return
            }
        }
        uiManager.Instance().showUi(config.UI.ui_car_info,this.game_status_parent!=null?this.game_status_parent:null, (node:Node)=>{
            let car_info_component = node.getComponent(car_info)
            car_info_component.initView(list_item.getData(),list_item.getIsJiesuo())
            car_info_component.initOperateNodeCallback((v:car_info)=>{
                // 装备
                if(this.current_select_list_item!=null) {
                    this.current_select_list_item.setUnselectedStatus()
                }
                list_item.setSelectedStatus()
                this.current_select_list_item = list_item
        
                if(this.m_click_item_cb!=null) {
                    this.m_click_item_cb(list_item)
                }
            },(v:car_info)=>{
                // 兑换(废弃,更改为直接弹出兑换视图,代码见上方👆🏻)
                let data = v.getData()
                if(data.temp_bag_list_item_data) {
                    GameManager.showExchangeCarView(data.id,data.temp_bag_list_item_data.icon,data.temp_bag_list_item_data.quantity, ()=>{
                        car_info_component.closeSelf()
                        list_item.setJiesuoSelectedStatus()
                    })
                }
            })
            car_info_component.initGetSuipianNodeCallback((v:car_info)=>{
                // 看视频
                if(userDataManager.isGetCarDebrisLookVideo()==false) {
                    return uiManager.showToast('今日看视频已到上限')
                }
                GameManager.showVideoAd(config.ADS_TYPE.GAME_GET_SUIPIAN, ()=>{
                    userDataManager.addGetCarDebrisLookVideoNumber(v.getData().id, (data)=>{
                        this.getCarDebrisOperation(data, list_item, v)
                    })
                }) 
            }, (v:car_info)=>{
                // 分享
                if(userDataManager.isGetCarDebrisShare()==false) {
                    return uiManager.showToast('今日分享已到上限')
                }
                SdkUtil.shareGame("",tools.sys_config.share_des,(r)=>{
                    if(r==true) {
                        userDataManager.addGetCarDebrisShareNumber(v.getData().id, (data)=>{
                            this.getCarDebrisOperation(data, list_item, v)
                        })
                    }
                })
            })
        })
    }

    private getCarDebrisOperation(data, list_item:car_lib_list_item, v:car_info) {
        // console.log('data=',data)
        let car_info_data = v.getData()
        car_info_data.temp_bag_list_item_data.quantity+=data.quantity

        // 解锁了
        if(car_info_data.temp_bag_list_item_data.quantity>=car_info_data.unlock_points) {
            // 关闭车信息
            v.getComponent(car_info).closeSelf()
            // 列表更新数据
            this.requestData()
            // 兑换弹框
            GameManager.showExchangeCarView(car_info_data.id, data.icon, car_info_data.temp_bag_list_item_data.quantity, ()=>{
                list_item.setJiesuoSelectedStatus()
            })
            return
        }

        // 奖励弹框
        uiManager.Instance().showUi(config.UI.ui_reward_tips_view, null, (node:Node)=>{
            node.getComponent(reward_tips_view).initView([data])
            
            // 车信息更新数据
            v.initView(car_info_data,v.getIsJiesuo())
            v.setCarSuipianData()

            // 列表更新数据
            this.requestData()
        })
    }

    public initGameStatusParent(parent:Node){
        this.game_status_parent = parent
    }
}

