package com.itant.shiwushu.tool;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import com.itant.shiwushu.manager.ToastManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Jason on 2018/10/3.
 */

public class StringTool {
    /**
     * 半角转换为全角，让字体对齐
     */
    public static String stringToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                // 全角空格为12288，半角空格为32
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                // 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    private static final String LABEL_COPY = "lost";
    public static void copyText(Context context, String text) {
        ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText(LABEL_COPY, text);
        if (manager != null) {
            manager.setPrimaryClip(clipData);
            ToastManager.getInstance().toastShort("复制成功");
        } else {
            ToastManager.getInstance().toastShort("复制失败");
        }
    }

    public static String getStringFromAssets(Context context, String fileName) throws IOException {
        InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName),"UTF-8");
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line;
        StringBuilder builder = new StringBuilder();
        while((line = bufReader.readLine()) != null) {
            builder.append(line);
        }
        return builder.toString();
    }


    /**
     * 从raw文件读取字符串
     * @param context
     * @param rawFileId
     * @return
     * @throws IOException
     */
    public static String getStringFromRaw(Context context, int rawFileId) throws IOException{
        InputStreamReader inputReader = new InputStreamReader(context.getResources().openRawResource(rawFileId),"UTF-8");
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line;
        StringBuilder builder = new StringBuilder();
        while((line = bufReader.readLine()) != null) {
            builder.append(line);
        }

        return builder.toString();

    }

}
