/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.database

import com.sybotan.base.extensions.*
import com.sybotan.base.utils.SJsonUtil
import org.jetbrains.anko.db.MapRowParser
import java.io.Serializable
import java.lang.reflect.ParameterizedType
import javax.persistence.Column

/**
 * 记录行解析器
 *
 * @author  庞利祥(sybotan@126.com)
 */
open class SRowParser<out ENTITY: Any>(var clz: Class<*>? = null) : MapRowParser<ENTITY> {

    /**
     * 解析记录行
     *
     * @param   columns     字段映射
     * @return  实体类
     */
    override fun parseRow(columns: Map<String, Any?>): ENTITY {
        if (clz == null) {
            clz = gennericClass()
        }

        val entity = clz!!.newInstance() as ENTITY
        for (field in clz!!.getAllDeclaredFields()) {
            if (field.isAnnotationPresent(Column::class.java)) {
                val anno = field.getAnnotation(Column::class.java)
                field.isAccessible = true
                val value = columns[anno.name] ?: continue
                try {
                    field.set(entity, when (field.type.simpleName) {
                        "byte", "Byte" -> value.toByte()
                        "double", "Double" -> value.toDouble()
                        "float", "Float" -> value.toFloat()
                        "int", "Integer" -> value.toInt()
                        "long", "Long" -> value.toLong()
                        "short", "Short" -> value.toShort()
                        "Boolean" -> value.toBoolean()
                        "String" -> value.toString()
                        "List", "ArrayList" -> {
                            val type = (field.genericType as ParameterizedType).actualTypeArguments[0]
                            //SJsonUtil.fromJsonAsList<type>(value.toString())
                            null
                        }
                        "HashMap", "Map" -> {
                            //val type = object : TypeToken<HashMap<Any, Any>>(){}.type
                            SJsonUtil.fromJson<HashMap<Any, Any>>(value.toString())
                        }
                        else ->{
                             // SJsonUtil.fromJson<field.javaClass>(value.toString())
                            null
                        }
                    })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                field.isAccessible = false
            }
        }
        return entity
    } // Function parseRow()
} // Class SRowParser