// 一些组件公用函数
import {Method, request} from "./interface";
import {ActivityDetail, ActivityStatus, UserType} from "./entity";
import {
    ChangeActivityRes,
    ChangeActivityTransform,
    PageDataMessage,
    SimpleMessage,
    UserRes,
    UserTransform
} from "./result";
import React from "react";
import {user_cookie, user_type_cookie} from "./account/PropCookie";
import {Cookies} from "react-cookie";
import {Api} from "./api";

/**
 * 更新活动状态
 * @param activityId
 * @param activityStatus
 * @param that
 * @param callback
 */
export function changeActivity(activityId:number,activityStatus:ActivityStatus,that:React.Component,callback:Function){

    request(Api.main.activity.apply,Method.POST,{
        activityId:activityId+"",
        activityStatus:activityStatus.toString()
    },new ChangeActivityTransform(),function (res:ChangeActivityRes) {
        switch (res.customResult) {
            case SimpleMessage.ok:
                callback(res.timeScore)
                that.setState({
                    result:<div>
                                <h3 className="text-info text-center">{getStatus(activityStatus)}成功,{ActivityStatus.complete===activityStatus?'关闭此窗口将刷新时间币':null}</h3>
                            </div>
                });break
            case SimpleMessage.fail:
                that.setState({
                    result:<h3 className="text-danger text-center">{getStatus(activityStatus)}失败，请联系管理员</h3>
        });break
    }
    })
}

//活动状态转换
export function getStatus(status:ActivityStatus){
    switch (status) {
        case ActivityStatus.apply:return "报名"
        case ActivityStatus.join:return "参与"
        case ActivityStatus.complete:return "完成"
    }
}

/**
 * 滚动到底部
 * @param e 滚动事件
 * @param that
 * @param callback 回调函数
 */
export function scrollBottom(e:any,that:React.Component<any,any>,callback:Function) {
    if (e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight) {

        if(that.state.page){
            if(that.state.page.currentPage===that.state.page.totalPage){
                that.setState({
                    scrollTip:'已滚动到底部',
                })
            }else{
                that.setState({
                    scrollTip:'尝试加载更多留言',
                })
                callback()
            }
        }else{
            that.setState({
                scrollTip:'丢失分页数据，请联系管理员',
            })
        }
    }
}

//查找我的好友
export function loadMyFriend(name:string,that:React.Component<any,any>,page:number){
    request(Api.account.myFriend,Method.GET, {
        currentPage:String(page),
        name:name
    },new UserTransform(),function (res:UserRes) {
        switch (res.customResult) {
            case PageDataMessage.ok:
                that.setState({
                    friendList: (that.state.friendList||[]).concat(res.dataList||[]),
                    page: res.page,
                    scrollTip: page > 1 ? '成功加载第' + page + '页数据' : null,
                })
                break
            case PageDataMessage.fail:
                that.setState({
                    result:<h3 className="text-danger text-center">查找好友失败，请联系管理员</h3>
                })
        }
    })

}

//参与活动提示
export function showActivityTip(activity:ActivityDetail, that:React.Component<{ cookies:Cookies },any>,button:JSX.Element){
    if(that.props.cookies.get(user_type_cookie)===UserType.seekHelp){
        return null
    }else if(activity.userId===that.props.cookies.get(user_cookie)){
        return <span className="text-danger text-center">我发布的活动</span>
    }else if(activity.hasApply){
        return <span className="text-danger text-center">已报名</span>
    }else if(activity.hasStart){
        return <span className="text-danger text-center">活动已开始</span>
    }else{
        return button
    }
}
