package cc.winboll.studio.webpagesources.common;

/**
 * @Author ZhanGSKen@QQ.COM
 * @Date 2024/08/26 16:16:00
 * @Describe 网站登录验证工具类
 */
import android.content.Context;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.dialogs.YesNoAlertDialog;
import cc.winboll.studio.webpagesources.models.AuthenticationBean;
import java.io.File;
import java.util.ArrayList;

public class AuthenticationUtils {

    public static final String TAG = "AuthenticationUtils";

    static AuthenticationUtils _mAuthenticationUtils;
    static String _mBeanPath;
    Context mContext;
    ArrayList<AuthenticationBean> mData;

    AuthenticationUtils(Context context) {
        mContext = context;
        File beanDir = new File(context.getFilesDir(), "home" + File.separator + TAG);
        if (!beanDir.exists()) {
            beanDir.mkdirs();
        }
        LogUtils.d(TAG, String.format("beanDir %s", beanDir.toString()));
        _mBeanPath = beanDir.getPath() + "/" + AuthenticationBean.class.getName() + ".json";
        mData = new ArrayList<AuthenticationBean>();
        AuthenticationBean.loadBeanListFromFile(_mBeanPath, mData, AuthenticationBean.class);
    }

    public static AuthenticationUtils getInstance(Context context) {
        if (_mAuthenticationUtils == null) {
            _mAuthenticationUtils = new AuthenticationUtils(context);
        }
        return _mAuthenticationUtils;
    }

    ArrayList<AuthenticationBean> loadAuthenticationData(String szHost, String szRealm) {
        ArrayList<AuthenticationBean> listReturn = new ArrayList<AuthenticationBean>();
        for (AuthenticationBean item : getInstance(mContext).mData) {
            if (szHost.equals(item.getHost())
                && szRealm.equals(item.getRealm())) {
                listReturn.add(item);
            }
        }
        return listReturn;
    }

    public ArrayList<AuthenticationBean> getHostAuthenticationList(String szHost, String szRealm) {
        ArrayList<AuthenticationBean> listReturn = new ArrayList<AuthenticationBean>();
        for (final AuthenticationBean item : mData) {
            if (szHost.equals(item.getHost())
                && szRealm.equals(item.getRealm())) {
                listReturn.add(item);
            }
        }
        return listReturn;
    }

    public void deleteAuthenticationInfo(AuthenticationBean bean) {
        for (final AuthenticationBean item : mData) {
            if (bean.getHost().equals(item.getHost())
                && bean.getRealm().equals(item.getRealm())
                && bean.getUserName().equals(item.getUserName())) {
                mData.remove(item);
                AuthenticationBean.saveBeanListToFile(_mBeanPath, mData);
                return;
            }
        }
    }

    public void putAuthenticationBeanToTheFirstPosition(AuthenticationBean bean) {
        for (AuthenticationBean item : mData) {
            if (bean.getHost().equals(item.getHost())
                && bean.getRealm().equals(item.getRealm())
                && bean.getUserName().equals(item.getUserName())) {
                mData.add(0, new AuthenticationBean(item));
                mData.remove(item);
                AuthenticationBean.saveBeanListToFile(_mBeanPath, mData);
                return;
            }
        }
    }

    public void saveAuthenticationInfo(AuthenticationBean bean) {
        saveAuthenticationInfo(bean.getHost(), bean.getRealm(), bean.getUserName(), bean.getPassword());
    }

    public void saveAuthenticationInfo(String szHost, String szRealm, String szUserName, final String szPassword) {
        boolean isFindUserName = false;
        AuthenticationBean.loadBeanListFromFile(_mBeanPath, mData, AuthenticationBean.class);
        for (final AuthenticationBean item : mData) {
            if (szHost.equals(item.getHost())
                && szRealm.equals(item.getRealm())
                && szUserName.equals(item.getUserName())) {
                isFindUserName = true;
                if (!szPassword.equals(item.getPassword())) {
                    // 如果找到同名信息，就提示是否更新
                    YesNoAlertDialog.show(mContext, "Ask Update Password", "Is update " + item.getUserName() + " Password?", new YesNoAlertDialog.OnDialogResultListener(){
                            @Override
                            public void onNo() {
                                return;
                            }

                            @Override
                            public void onYes() {
                                item.setPassword(szPassword);
                                AuthenticationBean.saveBeanListToFile(_mBeanPath, mData);
                                putAuthenticationBeanToTheFirstPosition(item);
                                return;
                            }
                        });
                } else {
                    putAuthenticationBeanToTheFirstPosition(item);
                    return;
                }
            }
        }

        // 如果找不到同名信息，就新建一个
        if (!isFindUserName) {
            AuthenticationBean bean = new AuthenticationBean(szHost, szRealm, szUserName, szPassword);
            mData.add(bean);
            AuthenticationBean.saveBeanListToFile(_mBeanPath, mData);
            putAuthenticationBeanToTheFirstPosition(bean);
        }
    }
}
