package jp.odakyu.toiletsignage.logic;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jp.odakyu.toiletsignage.file.FileUtil;
import jp.odakyu.toiletsignage.info.ToiletInfo;
import jp.odakyu.toiletsignage.json.ToiletDefaultJson;

import static java.lang.Boolean.FALSE;

/**
 * Created by agentec on 2017/12/08.
 */

public class MainLogic {
    private static final String TOILET_INFO_JSON = "/toiletInfo.json";
    private static final String KEY_TOILET_ARRAY = "toiletArray";
    private static final String TAG = "MainLogic";

    public void getToiletDefaultInfo(Context context) throws Exception {
        String []rootStorageDirectory = getExtSdCardPathsForActivity(context);

        File downloadFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        String copyFilePath = downloadFilePath.getPath() + TOILET_INFO_JSON;

        for(String directoryName:rootStorageDirectory) {
            String jsonFilePath = directoryName + TOILET_INFO_JSON;
            Log.d(TAG, jsonFilePath);
            File uspJsonFile = new File(jsonFilePath);

            if (uspJsonFile.exists() && uspJsonFile.isFile()) {
                FileUtil.fileCopy(jsonFilePath, copyFilePath);
                break;
            } else {
                Log.e(TAG, "usb json file not exists = " + jsonFilePath);
            }
        }

        if (new File(copyFilePath).exists()) {
            JSONObject jsonObject = new JSONObject(FileUtil.getStringFromFile(copyFilePath));;
            JSONArray jsonArray = jsonObject.getJSONArray(KEY_TOILET_ARRAY);
            ArrayList<ToiletDefaultJson> toiletDefaultJsonArray = new ArrayList<ToiletDefaultJson>();
            boolean isValidation = FALSE;
            for (int i = 0; i < jsonArray.length(); i++){
                ToiletDefaultJson toiletDefaultJson = new ToiletDefaultJson(jsonArray.getJSONObject(i));
                isValidation = toiletDefaultJson.validation(jsonArray.getJSONObject(i));

                if (isValidation) {
                    toiletDefaultJsonArray.add(toiletDefaultJson);
                } else {
                    break;
                }
            }
            ToiletInfo.getInstance().setValidation(isValidation);
            if (toiletDefaultJsonArray.size() != 0 && isValidation) {
                ToiletInfo.getInstance().setToiletDefaultJsonArray(toiletDefaultJsonArray);
            }
        }
    }

    public static String[] getExtSdCardPathsForActivity(Context context) {
        List<String> paths = new ArrayList<>();
        for (File file : context.getExternalFilesDirs("external")) {
            if (file != null) {
                int index = file.getAbsolutePath().lastIndexOf("/Android/data");
                if (index < 0) {
                    Log.w("AmazeFileUtils", "Unexpected external file dir: " + file.getAbsolutePath());
                } else {
                    String path = file.getAbsolutePath().substring(0, index);
                    try {
                        path = new File(path).getCanonicalPath();
                    } catch (IOException e) {
                        // Keep non-canonical path.
                    }
                    paths.add(path);
                }
            }
        }
        if (paths.isEmpty()) paths.add("/storage/sdcard1");
        return paths.toArray(new String[0]);
    }
}
