package com.sheep.gamegroup.model.entity;

import android.text.TextUtils;

import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.helper.DownloadHelper;
import com.sheep.gamegroup.model.api.IDownload;

import java.io.Serializable;
import java.util.List;

/**
 * Created by ljy on 2018/9/4.
 */

public class GameEntity implements Serializable {
    private int accepted_task_id;//integer($int64)领取福利时使用

    private Applications app;
    private boolean can_download;//boolean
    private int is_reservation;//integer($int64)0 不是 1是预约

    private boolean received_voucher;//boolean是否已经领取了福利

    private int release_task_id;//integer($int64)发布任务id

    private int status;//integer($int64)福利任务时使用，和任务记录一样

    private Release_task release_task;

    private List<GiftBagApp> has_gift_bag;

    //    IsFocusGame bool                `json:"is_focus_game" description:"是否关注游戏"`
    private boolean is_focus_game;//是否已经关注了游戏

    public Release_task getRelease_task() {
        return release_task;
    }

    public void setRelease_task(Release_task release_task) {
        this.release_task = release_task;
    }

    public List<GiftBagApp> getHas_gift_bag() {
        return has_gift_bag;
    }

    public void setHas_gift_bag(List<GiftBagApp> has_gift_bag) {
        this.has_gift_bag = has_gift_bag;
    }

    public int getAccepted_task_id() {
        return accepted_task_id;
    }

    public void setAccepted_task_id(int accepted_task_id) {
        this.accepted_task_id = accepted_task_id;
    }

    public Applications getApp() {
        return app;
    }

    public void setApp(Applications app) {
        this.app = app;
    }

    public boolean getCan_download() {
        return can_download;
    }

    public void setCan_download(boolean can_download) {
        this.can_download = can_download;
    }

    public int getIs_reservation() {
        return is_reservation;
    }

    public void setIs_reservation(int is_reservation) {
        this.is_reservation = is_reservation;
    }

    public boolean getReceived_voucher() {
        return received_voucher;
    }

    public void setReceived_voucher(boolean received_voucher) {
        this.received_voucher = received_voucher;
    }

    public int getRelease_task_id() {
        return release_task_id;
    }

    public void setRelease_task_id(int release_task_id) {
        this.release_task_id = release_task_id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public boolean isIs_focus_game() {
        return is_focus_game;
    }

    public void setIs_focus_game(boolean is_focus_game) {
        this.is_focus_game = is_focus_game;
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof GameEntity && (((release_task != null && release_task.equals(((GameEntity) obj).release_task))
                || (app != null && app.equals(((GameEntity) obj).app))));
    }

    /**
     * 是否有礼包
     *
     * @return
     */
    public boolean isGift() {
        return has_gift_bag != null && has_gift_bag.size() > 0;
    }

    /**
     * 获取奖金的文本表示（这里保留两位有效数字，因为服务器可能返回这样的结果：0.6000000238418579）
     *
     * @return
     */
    public String getBonusText() {
        if (release_task != null) {
            return NumberFormatUtils.retainMost2(release_task.getBonus());
        } else {
            return null;
        }
    }

    /**
     * 是否显示整个礼包和福利金额标签
     * trur:不显示
     */
    public boolean isGiftAndBonus() {
        return !isGift() && TextUtils.isEmpty(getBonusText());
    }

    /**
     * 是否游戏任务
     */
    public boolean isGameTask() {
        return release_task != null && release_task.getTask() != null;
    }

    public String downLinks() {
        try {
            if (isGameTask()) {
                return release_task.getTask().getDownload_link();
            } else {
                return app.getDownload_url();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //根据包名获取下载地址
    public String getDownloadUrl(String packageName) {
        IDownload download = getDownload();
        return download != null && TextUtils.equals(download.getPackage_names(), packageName) ? download.getDownload_link() : null;
    }

    public IDownload getDownload() {
        try {
            if (isGameTask()) {
                return release_task.getTask();
            }
            if (app != null) {
                return app;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据下载地址获取对应的downloadHelper
    public DownloadHelper getDownloadHelper(String downloadUrl) {
        try {
            if (isGameTask() && TextUtils.equals(downloadUrl, release_task.getTask().getDownload_link())) {
                return release_task.getDownloadHelper();
            }
            if (app != null && TextUtils.equals(downloadUrl, app.getDownload_link())) {
                return app.getDownloadHelper();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
