package com.miya.fastcashier.utils.manage;

import android.Manifest;
import android.os.Environment;
import android.text.TextUtils;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.miya.fastcashier.service.AccountService;
import com.permissionx.guolindev.PermissionX;

import java.util.concurrent.Executors;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 类描述：账户管理员密码管理
 * 概述：
 * 创建人：zpxiang
 * 创建时间：2022/4/1
 * 修改人：
 * 修改时间：
 */
public class AccountPasswordManageKit {
    private static final String FileDir = Environment.getExternalStorageDirectory() + "/fastcashier/";
    public static final String ACCOUNT_DEFAULT_PASSWORD = "123456";

    private static AccountPasswordManageKit sInstance;

    private AccountPasswordManageKit() {
    }

    public interface AccountPasswordManageCallback {
        void onPasswordGet(String password);
    }

    public static AccountPasswordManageKit getInstance() {
        if (sInstance == null) {
            sInstance = new AccountPasswordManageKit();
        }
        return sInstance;
    }

    public void checkAccountPassword(AppCompatActivity context) {
        String accountPassword = AccountService.INSTANCE.getAccountPassword();
        if (!TextUtils.isEmpty(accountPassword)) return;
        PermissionX.init(context)
                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        initAccountPassword(context,null);
                    } else {
                        ToastUtils.showShort("请检查权限后重试");
                    }
                });
    }

    public String getCurrentAccountFilePath(){
        return FileDir.concat(AccountService.INSTANCE.getUserName());
    }

    public void initAccountPassword(AppCompatActivity context,AccountPasswordManageCallback callback) {
        LogUtils.e("path", getCurrentAccountFilePath());
        if (FileUtils.isFileExists(getCurrentAccountFilePath())) {
            Executors.newCachedThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    String password = FileIOUtils.readFile2String(getCurrentAccountFilePath());
                    AccountService.INSTANCE.setAccountPassword(password);
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (callback != null){
                                callback.onPasswordGet(password);
                            }
                            LogUtils.e("FileExists",password);
                        }
                    });
                }
            });
            return;
        }

        AccountService.INSTANCE.setAccountPassword(ACCOUNT_DEFAULT_PASSWORD);
        if (callback != null){
            callback.onPasswordGet(ACCOUNT_DEFAULT_PASSWORD);
            LogUtils.e("FileNoExists",ACCOUNT_DEFAULT_PASSWORD);
        }
        if (FileUtils.createOrExistsFile(getCurrentAccountFilePath())) {
            Executors.newCachedThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    LogUtils.e("creat File",ACCOUNT_DEFAULT_PASSWORD);
                    FileIOUtils.writeFileFromString(getCurrentAccountFilePath(), ACCOUNT_DEFAULT_PASSWORD);
                }
            });
        }
    }

    public void loadAccountPassword(AppCompatActivity context,AccountPasswordManageCallback callback) {
        String accountPassword = AccountService.INSTANCE.getAccountPassword();
        if (TextUtils.isEmpty(accountPassword)){
            PermissionX.init(context)
                    .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE)
                    .request((allGranted, grantedList, deniedList) -> {
                        if (allGranted) {
                            initAccountPassword(context,callback);
                        } else {
                            ToastUtils.showShort("请检查权限后重试");
                        }
                    });
        }else {
            if (callback != null){
                callback.onPasswordGet(accountPassword);
                LogUtils.e("loadAccountPassword",accountPassword);
            }
        }
    }

    public void updateAccountPassword(String password) {
        if (TextUtils.isEmpty(password))return;
        AccountService.INSTANCE.setAccountPassword(password);
        if (FileUtils.isFileExists(getCurrentAccountFilePath())) {
            FileUtils.deleteAllInDir(getCurrentAccountFilePath());
        }

        if (FileUtils.createOrExistsFile(getCurrentAccountFilePath())) {
            Executors.newCachedThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    FileIOUtils.writeFileFromString(getCurrentAccountFilePath(), password);
                }
            });
        }
    }
}
