package xn.hxp.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import xn.hxp.R

import java.io.Serializable

object UiManager {
    /**
     * Activity不带数据的跳转
     *
     * @param context
     * @param cls
     */
    fun switcher(context: Context, cls: Class<*>?) {
        val intent = Intent()
        intent.setClass(context, cls!!)
        context.startActivity(intent)
        //设置切换动画，从右边进入，左边退出
        if (context is Activity) {
            context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out)
        }
    }

    /**
     * Activity带有数据跳转
     *
     * @param context
     * @param map
     * @param cls
     */
    fun switcher(context: Context, map: Map<String, Any>?, cls: Class<*>?) {
        val intent = Intent()
        putExtraData(map, intent)
        intent.setClass(context, cls!!)
        context.startActivity(intent)
        if (context is Activity) {
            context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out)
        }
    }

    /**
     * Activity带返回值的跳转
     *
     * @param context
     * @param cls
     */
    fun switcher(context: Activity, cls: Class<*>?, requestCode: Int) {
        val intent = Intent()
        intent.setClass(context, cls!!)
        context.startActivityForResult(intent, requestCode)
        //设置切换动画，从右边进入，左边退出
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out)
    }

    /**
     * Activity带有数据并且有返回值的跳转
     *
     * @param context
     * @param map
     * @param requestCode
     */
    fun switcher(context: Activity, map: Map<String, Any>?, cls: Class<*>?, requestCode: Int) {
        val intent = Intent()
        putExtraData(map, intent)
        intent.setClass(context, cls!!)
        context.startActivityForResult(intent, requestCode)
        //设置切换动画，从右边进入，左边退出
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out)
    }

    /**
     * 添加数据
     *
     * @param map
     * @param intent
     */
    private fun putExtraData(map: Map<String, Any>?, intent: Intent) {
        if (map != null) {
            val keys = map.keys.iterator()
            while (keys.hasNext()) {
                val key = keys.next()
                when (val value = map[key]) {
                    is Int -> {
                        intent.putExtra(key, value as Int?)
                    }

                    is String -> {
                        intent.putExtra(key, value as String?)
                    }

                    is Double -> {
                        intent.putExtra(key, value as Double?)
                    }

                    is Float -> {
                        intent.putExtra(key, value as Float?)
                    }

                    is Long -> {
                        intent.putExtra(key, value as Long?)
                    }

                    is Boolean -> {
                        intent.putExtra(key, value as Boolean?)
                    }

                    is Any -> {
                        intent.putExtra(key, value as Parcelable?)
                    }

                    is Any -> {
                        intent.putExtra(key, value as Serializable?)
                    }

                }
            }
        }
    }
}