package com.miya.fastcashier.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.fastcashier.lib_common.BuildConfig
import com.fastcashier.lib_common.function.account.AccountService.getAccountInfo
import com.fastcashier.lib_common.net.ApiConfig.baseUrl
import com.fastcashier.lib_common.util.DateUtils
import com.fastcashier.lib_common.util.getVersion
import com.fastcashier.lib_common.util.getVersionCode
import com.fastcashier.lib_common.util.getWifyName
import com.miya.fastcashier.R
import com.miya.fastcashier.databinding.DialogSystemParameterBinding
import com.miya.fastcashier.util.manage.ChannelManageKit
import com.miya.print.PrinterManager
import java.util.*

class SystemParameterDialog(context: Context) : Dialog(context, R.style.CommonDialog) {

    private val viewBinding: DialogSystemParameterBinding

    init {
        viewBinding = DialogSystemParameterBinding.inflate(
            layoutInflater
        )
        setContentView(viewBinding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(true)
        init()
    }

    private fun init() {
        val accountInfo = getAccountInfo()
        setInfo(viewBinding.tvVersion, getVersion(context))
        setInfo(viewBinding.tvStoreName, accountInfo!!.shopInfo.storeName)
        setInfo(viewBinding.tvStoreNum, accountInfo.shopInfo.storeId)
        setInfo(viewBinding.tvMerchantNum, accountInfo.shopInfo.hhMerchant)
        setInfo(viewBinding.tvPos, accountInfo.shopInfo.posId)
        setInfo(viewBinding.tvCashier, accountInfo.shopInfo.operatorId)
        setInfo(viewBinding.tvVersionType, "fastCashier_" + BuildConfig.appType)
        setInfo(viewBinding.tvEquipment, "sunmi_v2pro")
        setInfo(viewBinding.tvServerUrl, baseUrl)
        setInfo(viewBinding.tvWifiName, getWifyName(context))
        setInfo(
            viewBinding.tvLog,
            "${getVersionCode(context)}_${DateUtils.format8(Date())}.txt"
        )
        setInfo(
            viewBinding.tvPrintType,
            if (PrinterManager.getInstance().printer == null) context.resources.getString(R.string.app_unkown) else PrinterManager.getInstance().printer.printerName
        )
        setInfo(viewBinding.tvChannel, ChannelManageKit.getAppChannelName(context))
        viewBinding.ivClose.setOnClickListener(View.OnClickListener { dismiss() })
        resize()
    }

    private fun setInfo(view: View, info: String?) {
        (view as TextView).text = info
    }

    /**
     * 设置对话框大小
     *
     * @param width  宽度，传0表示走默认宽度
     * @param height 高度，传0表示走默认高度
     */
    fun setSize(width: Int, height: Int): SystemParameterDialog {
        if (width < 0 || height < 0) {
            return this
        }
        val layoutParams = window!!.attributes
        if (width != 0 && height != 0) {
            layoutParams.width = width
            layoutParams.height = height
        } else if (width == 0 && height != 0) {
            layoutParams.height = height
        } else if (width != 0 && height == 0) {
            layoutParams.width = width
        }
        window!!.setGravity(Gravity.CENTER)
        window!!.attributes = layoutParams
        return this
    }

    private fun resize() {
        this.window!!.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT
        )
    }
}