package epson.scan.activity;

import android.content.Context;

import com.epson.lib.escani2.I2LibScannerInfoAndCapability;
import com.epson.mobilephone.common.wifidirect.MacAddrUtils;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.io.IOException;
import java.io.Serializable;

import epson.scan.i2lib.I2ScanParamManager;
import epson.scan.lib.ScanInfoStorage;
import epson.scan.lib.ScannerInfo;

public class ScannerPropertyWrapper implements Serializable {
    private int mEscIVersion;
    private I2LibScannerInfoAndCapability mI2LibScannerInfoAndCapability;
    private ScannerInfo mScannerInfo;
    private String mSimpleApSsid;

    private ScannerPropertyWrapper() {
        mEscIVersion = -1;
        mScannerInfo = null;
        mI2LibScannerInfoAndCapability = null;
    }

    public ScannerPropertyWrapper(ScannerInfo scannerInfo) {
        mEscIVersion = 1;
        mScannerInfo = scannerInfo;
        mI2LibScannerInfoAndCapability = null;
    }

    public ScannerPropertyWrapper(ScannerInfo scannerInfo, I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        mEscIVersion = 2;
        mScannerInfo = scannerInfo;
        mI2LibScannerInfoAndCapability = i2LibScannerInfoAndCapability;
    }

    public String getSimpleApSsid() {
        return mSimpleApSsid;
    }

    public void setSimpleApSsid(String str) {
        mSimpleApSsid = str;
    }

    public int getEscIVersion() {
        return mEscIVersion;
    }

    public I2LibScannerInfoAndCapability getI2ScannerAllInfo() {
        return mI2LibScannerInfoAndCapability;
    }

    public ScannerInfo getI1ScannerInfo() {
        return mScannerInfo;
    }

    public void saveData(Context context, boolean z) throws IOException {
        if (isPrinterSet()) {
            ScanInfoStorage.saveEscIVersion(context, getEscIVersion());
            if (mEscIVersion == 2) {
                i2ScannerInfoSaveSettings(context);
                I2ScanParamManager.saveI2AllInfo(context, mI2LibScannerInfoAndCapability);
                if (z) {
                    saveSimpleApSsid(context);
                }
            }
        }
    }

    private void saveSimpleApSsid(Context context) {
        String str = mSimpleApSsid;
        if (str == null || str.length() <= 0) {
            WiFiDirectManager.resetConnectInfo(context, WiFiDirectManager.DEVICE_TYPE_SCANNER);
        } else {
            WiFiDirectManager.setConnectInfo(context, mSimpleApSsid, WiFiDirectManager.DEVICE_TYPE_SCANNER, mScannerInfo.getModelName());
        }
    }

    public static ScannerPropertyWrapper load(Context context) {
        ScannerPropertyWrapper scannerPropertyWrapper = new ScannerPropertyWrapper();
        scannerPropertyWrapper.mEscIVersion = ScanInfoStorage.loadEscIVersion(context);
        scannerPropertyWrapper.loadScannerInfoConnectivitySettings(context);
        if (scannerPropertyWrapper.mEscIVersion == 2) {
            scannerPropertyWrapper.loadI2Data(context);
        }
        return scannerPropertyWrapper;
    }

    private void loadI2Data(Context context) {
        mI2LibScannerInfoAndCapability = I2ScanParamManager.loadI2AllInfo(context);
    }

    public void resetScanner() {
        mScannerInfo = ScannerInfo.getInstance();
        mEscIVersion = -1;
    }

    public static void clearSavedData(Context context) {
        ScannerPropertyWrapper scannerPropertyWrapper = new ScannerPropertyWrapper();
        scannerPropertyWrapper.resetScanner();
        scannerPropertyWrapper.i2ScannerInfoSaveSettings(context);
        ScanInfoStorage.saveEscIVersion(context, -1);
        I2ScanParamManager.deleteAllInfoFile(context);
    }

    private void loadScannerInfoConnectivitySettings(Context context) {
        mScannerInfo = ScannerInfo.getInstance();
        ScanInfoStorage.getInstance().loadScannerConnectivityInfo(context, mScannerInfo);
        mSimpleApSsid = WiFiDirectManager.getConnectInfo(context, WiFiDirectManager.DEVICE_TYPE_SCANNER);
    }

    public boolean isPrinterSet() {
        String modelName = getModelName();
        return modelName != null && !modelName.isEmpty();
    }

    public String getIp() {
        return mScannerInfo.getIp();
    }

    public String getModelName() {
        return mScannerInfo.getModelName();
    }

    public int getLocation() {
        return mScannerInfo.getLocation();
    }

    public String getScannerId() {
        return mScannerInfo.getScannerId();
    }

    private void i2ScannerInfoSaveSettings(Context context) {
        ScanInfoStorage.saveScannerConnectivityInfo(context, mScannerInfo.getModelName(), mScannerInfo.getIp(), mScannerInfo.getScannerId(), mScannerInfo.getLocation());
    }

    public void updateSetSimpleApSsid(Context context) {
        String curConnectInfo = WiFiDirectManager.getCurConnectInfo(context, MacAddrUtils.getMacAddressFromScannerId(mScannerInfo.getScannerId()));
        if (mScannerInfo.getLocation() != 1 || curConnectInfo == null) {
            mSimpleApSsid = null;
        } else {
            mSimpleApSsid = curConnectInfo;
        }
    }
}
