package com.miya.fastcashier.log;

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.miya.fastcashier.BuildConfig;
import com.miya.fastcashier.R;
import com.miya.fastcashier.beans.SelfCashierAccountInfo;
import com.miya.fastcashier.databinding.DialogSystemParameterBinding;
import com.miya.fastcashier.net.ApiConfig;
import com.miya.fastcashier.service.AccountService;
import com.miya.fastcashier.utils.BaseFunctionKt;
import com.miya.print.PrinterManager;

import androidx.annotation.NonNull;

public class SystemParameterDialog extends Dialog {

    private DialogSystemParameterBinding viewBinding;

    public SystemParameterDialog(@NonNull Context context) {
        super(context, R.style.CommonDialog);

        viewBinding = DialogSystemParameterBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
        setCanceledOnTouchOutside(false);
        setCancelable(true);

        init();
    }

    private void init() {
        SelfCashierAccountInfo accountInfo = AccountService.INSTANCE.getAccountInfo();

        setInfo(viewBinding.tvVersion, BaseFunctionKt.getVersion(getContext()));
        setInfo(viewBinding.tvStoreName, accountInfo.getShopInfo().getStoreName());
        setInfo(viewBinding.tvStoreNum, accountInfo.getShopInfo().getStoreId());
        setInfo(viewBinding.tvMerchantNum, accountInfo.getShopInfo().getHhMerchant());
        setInfo(viewBinding.tvPos, accountInfo.getShopInfo().getPosId());
        setInfo(viewBinding.tvCashier, accountInfo.getShopInfo().getOperatorId());
        setInfo(viewBinding.tvVersionType, "fastCashier_" + BuildConfig.appType);
        setInfo(viewBinding.tvEquipment, "sunmi_v2pro");
        setInfo(viewBinding.tvServerUrl, ApiConfig.getBaseUrl());
        setInfo(viewBinding.tvWifiName, BaseFunctionKt.getWifyName(getContext()));
        setInfo(viewBinding.tvNetIp, BaseFunctionKt.getNetIp(getContext()) == null ? "未知" : BaseFunctionKt.getNetIp(getContext()));
        setInfo(viewBinding.tvPrintType, PrinterManager.getInstance().getPrinter() == null ?
                getContext().getResources().getString(R.string.app_unkown) :
                PrinterManager.getInstance().getPrinter().getPrinterName());
        setInfo(viewBinding.tvChannel, BuildConfig.CHANNEL);

        viewBinding.ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        resize();
    }

    private void setInfo(View view, String info) {
        ((TextView) view).setText(info);
    }

    /**
     * 设置对话框大小
     *
     * @param width  宽度，传0表示走默认宽度
     * @param height 高度，传0表示走默认高度
     */
    public SystemParameterDialog setSize(int width, int height) {
        if (width < 0 || height < 0) {
            return this;
        }
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        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;
        }
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setAttributes(layoutParams);
        return this;
    }

    private void resize() {
        this.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
    }
}