package com.ycgis.macall.personalcenter.v.activity;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.anhui.police.market.callback.AbstractMarketResult;
import com.anhui.police.market.sdk.MarketConfigure;
import com.fri.libfriapkrecord.read.SignRecordTools;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.utils.LogUtils;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ActivityAboutBinding;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.rxjava.ResultObserver;
import com.ycgis.macall.personalcenter.p.rxjava.UpdateUiOnSubscribe;
import com.ycgis.macall.personalcenter.util.FileUtils;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AboutActivity extends BaseViewBindActivity<ActivityAboutBinding> {

    @Override
    protected ActivityAboutBinding getViewBinding() {
        return ActivityAboutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {

    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void start() {
        viewBinding.include.contentTitle.setText(R.string.about);
        viewBinding.include.leftImage.setImageDrawable(getDrawables(R.drawable.back_cliener));
        viewBinding.include.titleleft.setOnClickListener(v -> finish());

        String[] version = getVersion();
        viewBinding.aboutVersionNum.setText(version[0]);
        viewBinding.aboutVersionName.setText("V " + version[1]);
        String recordNumber = RuanseeApplication.getStringValue("NRRecordNumber");
        if (StringUtil.isNullOrEmpty(recordNumber) || recordNumber.contains("应用未备案") || recordNumber.contains("未获取")) {
            //读取备案号
            String apkPath = getNativeApkPath(getApplicationContext());
            recordNumber = SignRecordTools.readNumbers(apkPath);
            RuanseeApplication.saveData("NRRecordNumber", recordNumber);
        }
        viewBinding.tvBah.setText(String.format("全国注册备案号：%s", recordNumber));

        //3.0.20240111 没有添加检测更新
        MarketConfigure.detectAppUpdates(this, false, "", new AbstractMarketResult() {
            @Override
            public void onResult(String result, boolean upgrade) {
                LogUtils.w(result);
            }

            @Override
            public void onConfirm(int stateType) {
                LogUtils.w("stateType:" + stateType);

            }

            @Override
            public void onCancel() {
                LogUtils.w("onCancel");
            }

            @Override
            public void onWarning() {
                LogUtils.w("onWarning");
            }

            @Override
            public void onResult(String result) {
                LogUtils.w(result);
            }
        });
    }

    //获取系统内APK文件路径
    public static String getNativeApkPath(@NonNull final Context context) {
        String apkPath = null;
        try {
            final ApplicationInfo applicationInfo = context.getApplicationInfo();
            if (applicationInfo == null) {
                return null;
            }
            apkPath = applicationInfo.sourceDir;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return apkPath;
    }

    private void jsdx() {
        Observable.create(emitter -> {
            String s = FileUtils.initFilePath(getContext());
            File file = new File(s);
            if (!file.exists()) {
                throw new FileNotFoundException("没有找到指定文件夹");
            }
            long l = FileUtils.calculateFileSize(file);
            emitter.onNext("缓存文件大小： " + FileUtils.formatFileSize(l, FileUtils.FileSizeFormType.M));
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultObserver<Object>("") {
                    @Override
                    protected void onNext(String identifierKey, @NotNull Object data) {
                        ToastUtil.centered(getContext(), data.toString());
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                        ToastUtil.centered(getContext(), e.getMessage());
                    }

                    @Override
                    protected void onComplete(String identifierKey) {

                    }
                });
    }


    public String[] getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            return new String[]{String.valueOf(info.versionCode), info.versionName};
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}