import { __private, _decorator, Component, instantiate, Layout, Node, Prefab, UITransform, Widget } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { rewards_item_data } from '../../data';
import { reward_tips_item } from './reward_tips_item';
const { ccclass, property } = _decorator;

@ccclass('reward_tips_view')
export class reward_tips_view extends base_ui {
    @property(Node) public_bg:Node = null
    @property(Node) list_scrollView:Node = null
    @property(Node) list_content:Node = null
    @property(Prefab) reward_item:Prefab = null
    start() {
        this.onButtonListen(this.public_bg, ()=>{
            this.close()
        })
    }

    initView(data_list:rewards_item_data[]) {
        this.list_content.removeAllChildren()
        let count = data_list.length
        if(count==0) { return }
        for (let index = 0; index < data_list.length; index++) {
            const element = data_list[index];
            let item = instantiate(this.reward_item)
            item.parent = this.list_content
            item.getComponent(reward_tips_item).initView(element)
        }
        if(count<=4) {
            let item_width = instantiate(this.reward_item).getComponent(UITransform).width
            let list_size = this.list_content.getComponent(UITransform).contentSize
            let list_content_layout = this.list_content.getComponent(Layout)
            if(count<4) {
                // 水平
                let padding_horizontal = (list_size.width - item_width * count) / 2
                list_content_layout.paddingLeft = padding_horizontal
                list_content_layout.paddingRight = padding_horizontal
            }
            // 纵向
            let list_scrollview_size = this.list_scrollView.getComponent(UITransform).contentSize
            let list_content_layout_paddingTop_paddingBottom = list_content_layout.paddingTop + list_content_layout.paddingBottom
            this.list_scrollView.getComponent(Widget).top = ((list_scrollview_size.height - list_size.height) / 2) - list_content_layout_paddingTop_paddingBottom
            this.list_scrollView.getComponent(UITransform).height = list_size.height + list_content_layout_paddingTop_paddingBottom
        }
    }
}

