package com.jihe.punchnet

import android.os.Parcel
import android.os.Parcelable
import com.jihe.punchnet.data.RouteItem

data class RouteInfo(
    // 192.168.80.0/24
    val targetNetCIDR: String,
    // 10.167.69.2
    val gateway: String,
)

data class PunchnetServiceArgument(
    val token: String,
    val routes: Array<RouteItem>,
): Parcelable {
    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(token)
        dest.writeInt(routes.size)
        routes.forEach { item ->
            dest.writeLong(item.id)
            dest.writeInt(item.net_ip)
            dest.writeInt(item.mask_ip)
            dest.writeInt(item.gateway)
        }
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR: Parcelable.Creator<PunchnetServiceArgument> {
        override fun createFromParcel(source: Parcel): PunchnetServiceArgument {
            val token = source.readString()!!
            val size = source.readInt()
            var routes = mutableListOf<RouteItem>()
            for (i in 0..<size) {
                val id = source.readLong()!!
                val netIP = source.readInt()!!
                val maskIP = source.readInt()!!
                val gateway = source.readInt()!!

                routes.add(RouteItem(
                    id = id,
                    net_ip = netIP,
                    mask_ip = maskIP,
                    gateway = gateway,
                ))
            }
            return PunchnetServiceArgument(token, routes.toTypedArray())
        }

        override fun newArray(size: Int): Array<PunchnetServiceArgument?> {
            return arrayOfNulls(size)
        }
    }
}