package com.ahau.againstpest.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fanchunchun on 2018/3/27.
 */

public class LoginUtils {

    /**
     * 保存账户名，密码
     *
     * @param mContext 上下文
     * @param username 用户名
     * @param password 密码
     * @return
     */

    public static boolean saveUtil(Context mContext, String username, String password) {
        String userinfo = username + "##" + password;


        try {
            FileOutputStream fos = mContext.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
            fos.write(userinfo.getBytes());
            fos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param mContext
     * @return 提取账户名密码
     */
    public static Map<String, String> getUtil(Context mContext) {

        try {
            FileInputStream fis = mContext.openFileInput("userinfo.txt");
            BufferedReader buffer = new BufferedReader(new InputStreamReader(fis));
            String readLine = buffer.readLine();

            String[] split = readLine.split("##");
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("username", split[0]);
            map.put("password", split[1]);

            buffer.close();
            fis.close();

            return map;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
