import { _decorator, Component, EventTouch, instantiate, Layout, Node, PageView, Prefab, UITransform, Vec2 } from 'cc';
import { bag_list_item_data, car_item_data, car_type } from '../../data';
import { userDataManager } from '../../manager/userDataManager';
import { car_lib_list_item } from './car_lib_list_item';
import { dataManager } from '../../manager/dataManager';
const { ccclass, property } = _decorator;

@ccclass('car_lib_page_item')
export class car_lib_page_item extends Component {
    @property(Node) list:Node = null
    @property(Node) list_content:Node = null
    @property(Prefab) list_item:Prefab = null
    private m_pageView:Node = null
    private m_index = -1
    private m_data_list:car_item_data[] = []
    private m_click_cb = null

    private chuandi:boolean = true
    private startPosition:Vec2 = new Vec2(0,0)
    private movePosition:Vec2 = new Vec2(0,0)
    private endPosition:Vec2 = new Vec2(0,0)
    private pageIdx = 0

    protected start(): void {
        let list_content_size = this.list_content.getComponent(UITransform).contentSize
        let item_contenteSize = instantiate(this.list_item).getComponent(UITransform).contentSize
        let horizontal_padding = (list_content_size.width - item_contenteSize.width * 2) / 3
        if(horizontal_padding > 0) {
            this.list_content.getComponent(Layout).paddingLeft = horizontal_padding
            this.list_content.getComponent(Layout).paddingRight = horizontal_padding
            this.list_content.getComponent(Layout).spacingX = horizontal_padding
        }
        this.list.on(Node.EventType.TOUCH_START, this.touchStart, this)
        this.list.on(Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.list.on(Node.EventType.TOUCH_END, this.touchEnd, this)
        this.list.on(Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
    }

    public touchStart(event:EventTouch) {
        this.chuandi = true
        this.startPosition = event.getLocation()
        this.pageIdx = this.m_pageView.getComponent(PageView).getCurrentPageIndex()
    }

    private touchMove(event:EventTouch) {
        // console.log('touchMove=',event)
        if(this.chuandi == false) {
            return
        }
        this.chuandi = true
        this.movePosition = event.getLocation();
        let distance_x = this.movePosition.x - this.startPosition.x;
        let distance_y = this.movePosition.y - this.startPosition.y;
        // console.log("距离差== ", distance_x, distance_y);
        //判断是否需要翻页
        if (Math.abs(distance_x) > 50 && distance_x > 0) {
            // console.log("向前翻页");
            this.m_pageView.getComponent(PageView).scrollToPage(this.pageIdx - 1);
            this.chuandi = false;
        } else if (Math.abs(distance_x) > 50 && distance_x < 0) {
            // console.log("向后翻页");
            this.m_pageView.getComponent(PageView).scrollToPage(this.pageIdx + 1);
            this.chuandi = false;
        }
    }

    private touchEnd(event:EventTouch) {
        // console.log('touchEnd=',event)
        this.endPosition = event.getLocation();
        let distance_x = this.endPosition.x - this.startPosition.x;
        let distance_y = this.endPosition.y - this.startPosition.y;
        //判断是否是点击
        if (Math.abs(distance_y) < 50 && Math.abs(distance_x) < 50) {
        //   console.log("触摸结束，是点击");
        } else {
        //   console.log("结束1");
        }
    }

    init(pageView, index, data_list:car_item_data[], bag_car_suipian_list, selected_cb, click_cb) {
        this.m_pageView = pageView
        this.m_index = index
        this.m_data_list = data_list
        this.m_click_cb = click_cb
        this.list_content.removeAllChildren()
        for (let index = 0; index < data_list.length; index++) {
            const element = data_list[index];
            // 是否解锁
            var is_jiesuo = false

            // 汽车碎片逻辑
            if(element.stype==car_type.suipian) {
                if(dataManager.userCarListHavCar(element.id)) {
                    let on_bag = false
                    for (let i = 0; i < bag_car_suipian_list.length; i++) {
                        const i_e:bag_list_item_data = bag_car_suipian_list[i];
                        if(i_e.car_id==element.id) {
                            element.temp_bag_list_item_data = i_e
                            // bag_car_suipian_list.splice(i,1)
                            on_bag = true   
                            if(i_e.quantity>=element.unlock_points) {
                                is_jiesuo = true
                            }
                            break
                        }
                    }
                    if(on_bag==false) { //不在背包中,补充一个空的
                        element.temp_bag_list_item_data = new bag_list_item_data
                    }
                } else {  //不在用户车列表中,补充一个空的
                    element.temp_bag_list_item_data = new bag_list_item_data
                    // if(element.id==16) { element.temp_bag_list_item_data.quantity = 300} //用于测试
                }
            }

            // 业务逻辑
            let item = instantiate(this.list_item)
            item.parent = this.list_content
            let item_component = item.getComponent(car_lib_list_item)
            item_component.initView(element, index, this.onClickListItem.bind(this))
            if(element.stype==car_type.score) {
                is_jiesuo = userDataManager.user_car_list.car_list.some(obj => obj === element.id)  
            } 
            if(is_jiesuo) {
                item_component.setJiesuoSelectedStatus()
            } else {
                item_component.setJiesuoUnselectedStatus()
            }
            
            if(userDataManager.user_car_list.default_car_id == element.id) {
                item_component.setSelectedStatus()
                if(selected_cb) { selected_cb(this, item_component) }
            } else {
                item_component.setUnselectedStatus()
            }
        }
    }

    public getDataList():car_item_data[]{
        return this.m_data_list
    }

    public getIndex():number {
        return this.m_index
    }

    private onClickListItem(item:car_lib_list_item) {
        if(this.m_click_cb) {
            this.m_click_cb(this,item)
        }
    }
}

