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.common.CrDialogManager
import com.cr.dialog.DialogNormal
import com.cr.models.CompletionModel
import com.cr.models.ICompletion

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/30 11:31
 * 描述：窗体基类
 */
open class CrActivity : AppCompatActivity() {
    // define: 2023/3/10 创建主线程Looper
    protected var mainHandler = Handler(Looper.getMainLooper())
    // define: 2023/4/10 上下文
    protected var context:Context?= null

    /**
     * 扩展函数
     * @receiver FragmentManager
     * @param func [@kotlin.ExtensionFunctionType] Function1<FragmentTransaction, Unit>
     */
    private 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 warning String 警告消息
     */
    protected fun showWarning(warning: String) {
        CrDialogManager.getInstance().showWarning(warning)
    }

    /**
     * 显示错误信息
     * @param error String 错误消息
     */
    protected fun showError(error: String) {
        CrDialogManager.getInstance().showError(error)
    }

    /**
     * 确认对话框
     * @param message String 显示的消息内容
     * @param actionButtonContent Array<String> 按钮文字数组 大小必须是2
     * @param callback ICompletion<String> 回调 可以传null
     */
    protected fun showConfirm(message:String,actionButtonContent:Array<String>,callback: ICompletion<String>?){
        if(actionButtonContent.size != 2) return
        CrDialogManager.getInstance().showConfirm(message,actionButtonContent,callback)
    }

    /**
     * 显示提示信息
     * @param information String 提示消息
     */
    protected fun showInformation(information: String) {
        CrDialogManager.getInstance().showInformation(information)
    }

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

    }

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

    }
}