package com.miya.fastcashier.log

import android.app.Dialog
import android.content.Context
import com.miya.fastcashier.service.AccountService.getAccountInfo
import com.miya.fastcashier.utils.getVersion
import com.miya.fastcashier.net.ApiConfig.baseUrl
import com.miya.fastcashier.utils.getWifyName
import com.miya.fastcashier.utils.getNetIp
import com.miya.fastcashier.R
import com.miya.fastcashier.beans.SelfCashierAccountInfo
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.net.ApiConfig
import com.miya.print.PrinterManager
import android.widget.TextView
import com.miya.fastcashier.log.SystemParameterDialog
import android.view.WindowManager
import android.view.Gravity
import android.view.View
import com.miya.fastcashier.BuildConfig
import com.miya.fastcashier.databinding.DialogSystemParameterBinding

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.tvNetIp, if (getNetIp(context) == null) "未知" else getNetIp(context))
        setInfo(
            viewBinding.tvPrintType,
            if (PrinterManager.getInstance().printer == null) context.resources.getString(R.string.app_unkown) else PrinterManager.getInstance().printer.printerName
        )
        setInfo(viewBinding.tvChannel, BuildConfig.CHANNEL)
        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
        )
    }
}