package com.cr.cruav

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.cr.dialog.DialogNormal

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/30 11:31
 * 描述：窗体基类
 */
open class CrActivity : AppCompatActivity() {
    // define: 2023/4/10 信息标识符号
    val MSG_INFO_SHOW_DIALOG:Int = 1
    val MSG_ERROR_SHOW_DIALOG:Int =2
    // define: 2023/3/10 创建主线程Looper
    private var mainHandler = object:Handler(Looper.getMainLooper()){
        // todo: 2023/4/10 接收消息
        override fun handleMessage(msg: Message) {
            if(msg.what == MSG_INFO_SHOW_DIALOG){
                DialogNormal(context!!,"提示",msg.obj.toString()).show()
            }else if(msg.what == MSG_ERROR_SHOW_DIALOG){
                DialogNormal(context!!,"错误",msg.obj.toString()).show()
            }
        }
    }
    // define: 2023/4/10 上下文
    protected var context:Context?= null

    /**
     * 扩展函数
     * @receiver FragmentManager
     * @param func [@kotlin.ExtensionFunctionType] Function1<FragmentTransaction, Unit>
     */
    inline fun FragmentManager.inTransaction(func:FragmentTransaction.()->Unit){
        var fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }

    /**
     * 添加Fragment
     * @receiver FragmentActivity
     * @param fragment Fragment
     * @param frameId Int
     */
    fun FragmentActivity.addFragment(fragment:Fragment, frameId: Int){
        supportFragmentManager.inTransaction {
            add(frameId,fragment)
        }
    }

    /**
     * 隐藏Fragment
     * @receiver FragmentActivity
     * @param fragment Fragment
     */
    fun FragmentActivity.hideFragment(fragment:Fragment){
        supportFragmentManager.inTransaction {
            this.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            this.hide(fragment)
            this.addToBackStack("")
        }
    }

    /**
     * 显示Fragment
     * @receiver FragmentActivity
     * @param fragment Fragment
     */
    fun FragmentActivity.showFragment(fragment:Fragment){
        supportFragmentManager.inTransaction {
            //this.setCustomAnimations(R.anim.slide_left_in,R.anim.slide_left_out,R.anim.slide_left_in,R.anim.slide_left_out)
            this.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
            this.show(fragment)
            this.addToBackStack("")
        }
    }

    /**
     * 替换Fragment
     * @receiver FragmentActivity
     * @param fragment Fragment
     * @param frameId Int
     */
    fun FragmentActivity.replaceFragment(fragment:Fragment,frameId:Int){
        supportFragmentManager.inTransaction {
            replace(frameId,fragment)
        }
    }

    /**
     * 显示提示消息（用于多线程）
     * @param message String 消息内容
     */
    fun showMessage(message:String){
        var msg:Message = Message()
        msg.what = MSG_INFO_SHOW_DIALOG
        msg.obj = message
        mainHandler.sendMessage(msg)
    }

    /**
     * 显示错误消息（用于多线程）
     * @param error String 错误内容
     */
    fun showError(error:String){
        var msg:Message = Message()
        msg.what = MSG_INFO_SHOW_DIALOG
        msg.obj = error
        mainHandler.sendMessage(msg)
    }

    /**
     * 挂载控件
     */
    open fun joinControls() {

    }

    /**
     * 挂在事件
     */
    open fun registerListener() {

    }
}