package com.kfzs.duanduan.datashare.provider.session;

// @formatter:off

import com.kfzs.duanduan.data.graph.provider.current.Current;
import com.kfzs.duanduan.datashare.provider.base.BaseModel;

import java.util.Date;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * A session info which is part of a team.
 */
@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"})
public class SessionBean implements SessionModel {
    private long mId;
    private String mAccount;
    private String mOpenId;
    private long mUpdateTime;
    private String mToken;
    private Integer mUid;
    private Integer mHash;


    public SessionBean() {
    }

    public SessionBean(Current lastUser) {
        mId = lastUser.getMId();
        mAccount = lastUser.getMUserName();
        mOpenId = lastUser.getMOpenId();
        mUpdateTime = Long.parseLong(lastUser.getMUpdateTime());
        mToken = lastUser.getMToken();
        mUid = Integer.parseInt(lastUser.getMUserId());
    }


    /**
     * Primary key.
     */
    @Override
    public long getId() {
        return mId;
    }

    /**
     * Primary key.
     */
    public void setId(long id) {
        mId = id;
    }

    /**
     * account of this session. For instance, John.
     * Cannot be {@code null}.
     */
    @NonNull
    @Override
    public String getAccount() {
        return mAccount;
    }

    /**
     * account of this session. For instance, John.
     * Must not be {@code null}.
     */
    public void setAccount(@NonNull String account) {
        if (account == null)
            throw new IllegalArgumentException("account must not be null");
        mAccount = account;
    }

    /**
     * open_id of this session. For instance, John.
     * Can be {@code null}.
     */
    @Nullable
    @Override
    public String getOpenId() {
        return mOpenId;
    }

    /**
     * open_id of this session. For instance, John.
     * Can be {@code null}.
     */
    public void setOpenId(@Nullable String openId) {
        mOpenId = openId;
    }

    /**
     * update time of this session.
     */
    @Override
    public long getUpdateTime() {
        return mUpdateTime;
    }

    /**
     * update time of this session.
     */
    public void setUpdateTime(long updateTime) {
        mUpdateTime = updateTime;
    }

    /**
     * token of this session.
     * Can be {@code null}.
     */
    @Nullable
    @Override
    public String getToken() {
        return mToken;
    }

    /**
     * token of this session.
     * Can be {@code null}.
     */
    public void setToken(@Nullable String token) {
        mToken = token;
    }

    /**
     * uid at user of this session.
     * Can be {@code null}.
     */
    @Nullable
    @Override
    public Integer getUid() {
        return mUid;
    }

    /**
     * uid at user of this session.
     * Can be {@code null}.
     */
    public void setUid(@Nullable Integer uid) {
        mUid = uid;
    }

    /**
     * data hash save of this session.
     * Can be {@code null}.
     */
    @Nullable
    @Override
    public Integer getHash() {
        return mHash;
    }

    /**
     * data hash save of this session.
     * Can be {@code null}.
     */
    public void setHash(@Nullable Integer hash) {
        mHash = hash;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        SessionBean bean = (SessionBean) o;
        return mId == bean.mId;
    }

    @Override
    public int hashCode() {
        return (int) (mId ^ (mId >>> 32));
    }

    /**
     * Instantiate a new SessionBean with specified values.
     */
    @NonNull
    public static SessionBean newInstance(long id, @NonNull String account, @Nullable String openId, long updateTime, @Nullable String token, @Nullable Integer uid, @Nullable Integer hash) {
        if (account == null)
            throw new IllegalArgumentException("account must not be null");
        SessionBean res = new SessionBean();
        res.mId = id;
        res.mAccount = account;
        res.mOpenId = openId;
        res.mUpdateTime = updateTime;
        res.mToken = token;
        res.mUid = uid;
        res.mHash = hash;
        return res;
    }

    /**
     * Instantiate a new SessionBean with all the values copied from the given model.
     */
    @NonNull
    public static SessionBean copy(@NonNull SessionModel from) {
        SessionBean res = new SessionBean();
        res.mId = from.getId();
        res.mAccount = from.getAccount();
        res.mOpenId = from.getOpenId();
        res.mUpdateTime = from.getUpdateTime();
        res.mToken = from.getToken();
        res.mUid = from.getUid();
        res.mHash = from.getHash();
        return res;
    }

    public static class Builder {
        private SessionBean mRes = new SessionBean();

        /**
         * Primary key.
         */
        public Builder id(long id) {
            mRes.mId = id;
            return this;
        }

        /**
         * account of this session. For instance, John.
         * Must not be {@code null}.
         */
        public Builder account(@NonNull String account) {
            if (account == null)
                throw new IllegalArgumentException("account must not be null");
            mRes.mAccount = account;
            return this;
        }

        /**
         * open_id of this session. For instance, John.
         * Can be {@code null}.
         */
        public Builder openId(@Nullable String openId) {
            mRes.mOpenId = openId;
            return this;
        }

        /**
         * update time of this session.
         */
        public Builder updateTime(long updateTime) {
            mRes.mUpdateTime = updateTime;
            return this;
        }

        /**
         * token of this session.
         * Can be {@code null}.
         */
        public Builder token(@Nullable String token) {
            mRes.mToken = token;
            return this;
        }

        /**
         * uid at user of this session.
         * Can be {@code null}.
         */
        public Builder uid(@Nullable Integer uid) {
            mRes.mUid = uid;
            return this;
        }

        /**
         * data hash save of this session.
         * Can be {@code null}.
         */
        public Builder hash(@Nullable Integer hash) {
            mRes.mHash = hash;
            return this;
        }

        /**
         * Get a new SessionBean built with the given values.
         */
        public SessionBean build() {
            if (mRes.mAccount == null)
                throw new IllegalArgumentException("account must not be null");
            return mRes;
        }
    }

    public static Builder newBuilder() {
        return new Builder();
    }
}
