package com.ogove.hr.Core;

import android.content.SharedPreferences;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ogove.hr.Adapter.*;
import com.ogove.hr.Entities.*;
import com.ogove.hr.R;

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

import java.util.ArrayList;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

public final class LocalData {
    //*******************************************Company*******************************************
    public static ArrayList<Company> Companies = new ArrayList<Company>();

    public static void saveCompany(JSONObject jObj)
    {
        try {
            LocalStorage.save("Companies", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadCompanies();
    }

    public static void removeCompany(String Id)
    {
        try {
            LocalStorage.remove("Companies", Id);
        }
        catch (Exception E) { }
        loadCompanies();
    }

    public static void saveCompanies(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Companies", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadCompanies();
    }

    public static void loadCompanies()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Companies");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Companies.clear();
        Companies = Company.Parse(data);
    }

    public static Company getCompany(String Id)
    {
        for (int i = 0; i < Companies.size(); i++)
        {
            if (Companies.get(i).getId().equals(Id))
                return Companies.get(i);
        }
        return null;
    }

    //*******************************************Department*******************************************
    public static ArrayList<Department> Departments = new ArrayList<Department>();
    public static void saveDepartment(JSONObject jObj)
    {
        try {
            LocalStorage.save("Departments", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadDepartments();
    }

    public static void removeDepartment(String Id)
    {
        try {
            LocalStorage.remove("Departments", Id);
        }
        catch (Exception E) { }
        loadDepartments();
    }

    public static void saveDepartments(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Departments", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadDepartments();
    }

    public static void loadDepartments()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Departments");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Departments.clear();
        Departments = Department.Parse(data);
    }

    public static Department getDepartment(String Id)
    {
        for (int i = 0; i < Departments.size(); i++)
        {
            if (Departments.get(i).getId().equals(Id))
                return Departments.get(i);
        }
        return null;
    }
    
    //*******************************************Project*******************************************
    public static ArrayList<Project> Projects = new ArrayList<Project>();
    public static void saveProject(JSONObject jObj)
    {
        try {
            LocalStorage.save("Projects", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadProjects();
    }

    public static void removeProject(String Id)
    {
        try {
            LocalStorage.remove("Projects", Id);
        }
        catch (Exception E) { }
        loadProjects();
    }

    public static void saveProjects(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Projects", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadProjects();
    }

    public static void loadProjects()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Projects");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Projects.clear();
        Projects = Project.Parse(data);
    }

    public static Project getProject(String Id)
    {
        for (int i = 0; i < Projects.size(); i++)
        {
            if (Projects.get(i).getId().equals(Id))
                return Projects.get(i);
        }
        return null;
    }

    //*******************************************Boarding*******************************************
    public static ArrayList<Boarding> Boardings = new ArrayList<Boarding>();
    public static void saveBoarding(JSONObject jObj)
    {
        try {
            LocalStorage.save("Boardings", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadBoardings();
    }

    public static void removeBoarding(String Id)
    {
        try {
            LocalStorage.remove("Boardings", Id);
        }
        catch (Exception E) { }
        loadBoardings();
    }

    public static void saveBoardings(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Boardings", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadBoardings();
    }

    public static void loadBoardings()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Boardings");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Boardings.clear();
        Boardings = Boarding.Parse(data);
    }

    public static Boarding getBoarding(String Id)
    {
        for (int i = 0; i < Boardings.size(); i++)
        {
            if (Boardings.get(i).getId().equals(Id))
                return Boardings.get(i);
        }
        return null;
    }

    //*******************************************Manager*******************************************
    public static ArrayList<Manager> Managers = new ArrayList<Manager>();
    public static void saveManager(JSONObject jObj)
    {
        try {
            LocalStorage.save("Managers", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadManagers();
    }

    public static void removeManager(String Id)
    {
        try {
            LocalStorage.remove("Managers", Id);
        }
        catch (Exception E) { }
        loadManagers();
    }

    public static void saveManagers(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Managers", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadManagers();
    }

    public static void loadManagers()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Managers");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Managers.clear();
        Managers = Manager.Parse(data);
    }

    public static Manager getManager(String Id)
    {
        for (int i = 0; i < Managers.size(); i++)
        {
            if (Managers.get(i).getId().equals(Id))
                return Managers.get(i);
        }
        return null;
    }

    //*******************************************Member*******************************************
    public static ArrayList<Member> Members = new ArrayList<Member>();
    public static void saveMember(JSONObject jObj)
    {
        try {
            LocalStorage.save("Members", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadMembers();
    }

    public static void removeMember(String Id)
    {
        try {
            LocalStorage.remove("Members", Id);
        }
        catch (Exception E) { }
        loadMembers();
    }

    public static void saveMembers(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Members", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadMembers();
    }

    public static void loadMembers()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Members");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Members.clear();
        Members = Member.Parse(data);
    }

    public static Member getMember(String Id)
    {
        for (int i = 0; i < Members.size(); i++)
        {
            if (Members.get(i).getId().equals(Id))
                return Members.get(i);
        }
        return null;
    }

    //*******************************************Notification*******************************************
    public static ArrayList<Notification> Notifications = new ArrayList<Notification>();
    public static void saveNotification(JSONObject jObj)
    {
        try {
            LocalStorage.save("Notifications", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadNotifications();
    }

    public static void removeNotification(String Id)
    {
        try {
            LocalStorage.remove("Notifications", Id);
        }
        catch (Exception E) { }
        loadNotifications();
    }

    public static void saveNotifications(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Notifications", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadNotifications();
    }

    public static void loadNotifications()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Notifications");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Notifications.clear();
        Notifications = Notification.Parse(data);
    }

    public static Notification getNotification(String Id)
    {
        for (int i = 0; i < Notifications.size(); i++)
        {
            if (Notifications.get(i).getId().equals(Id))
                return Notifications.get(i);
        }
        return null;
    }

    //*******************************************Position*******************************************
    public static ArrayList<Position> Positions = new ArrayList<Position>();
    public static void savePosition(JSONObject jObj)
    {
        try {
            LocalStorage.save("Positions", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadPositions();
    }

    public static void removePosition(String Id)
    {
        try {
            LocalStorage.remove("Positions", Id);
        }
        catch (Exception E) { }
        loadPositions();
    }

    public static void savePositions(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Positions", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadPositions();
    }

    public static void loadPositions()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Positions");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Positions.clear();
        Positions = Position.Parse(data);
    }

    public static Position getPosition(String Id)
    {
        for (int i = 0; i < Positions.size(); i++)
        {
            if (Positions.get(i).getId().equals(Id))
                return Positions.get(i);
        }
        return null;
    }

    //*******************************************Task*******************************************
    public static ArrayList<Task> Tasks = new ArrayList<Task>();
    public static void saveTask(JSONObject jObj)
    {
        try {
            LocalStorage.save("Tasks", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadTasks();
    }

    public static void removeTask(String Id)
    {
        try {
            LocalStorage.remove("Tasks", Id);
        }
        catch (Exception E) { }
        loadTasks();
    }

    public static void saveTasks(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Tasks", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadTasks();
    }

    public static void loadTasks()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Tasks");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Tasks.clear();
        Tasks = Task.Parse(data);
    }

    public static Task getTask(String Id)
    {
        for (int i = 0; i < Tasks.size(); i++)
        {
            if (Tasks.get(i).getId().equals(Id))
                return Tasks.get(i);
        }
        return null;
    }

    //*******************************************User*******************************************
    public static ArrayList<User> Users = new ArrayList<User>();
    public static void saveUser(JSONObject jObj)
    {
        try {
            LocalStorage.save("Users", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadUsers();
    }

    public static void removeUser(String Id)
    {
        try {
            LocalStorage.remove("Users", Id);
        }
        catch (Exception E) { }
        loadUsers();
    }

    public static void saveUsers(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Users", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadUsers();
    }

    public static void loadUsers()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Users");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Users.clear();
        Users = User.Parse(data);
    }

    public static User getUser(String Id)
    {
        for (int i = 0; i < Users.size(); i++)
        {
            if (Users.get(i).getId().equals(Id))
                return Users.get(i);
        }
        return null;
    }

    //*******************************************Worker*******************************************
    public static ArrayList<Worker> Workers = new ArrayList<Worker>();
    public static void saveWorker(JSONObject jObj)
    {
        try {
            LocalStorage.save("Workers", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadWorkers();
    }

    public static void removeWorker(String Id)
    {
        try {
            LocalStorage.remove("Workers", Id);
        }
        catch (Exception E) { }
        loadWorkers();
    }

    public static void saveWorkers(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Workers", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadWorkers();
    }

    public static void loadWorkers()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Workers");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Workers.clear();
        Workers = Worker.Parse(data);
    }

    public static Worker getWorker(String Id)
    {
        for (int i = 0; i < Workers.size(); i++)
        {
            if (Workers.get(i).getId().equals(Id))
                return Workers.get(i);
        }
        return null;
    }

    //*******************************************Working*******************************************
    public static ArrayList<Working> Workings = new ArrayList<Working>();
    public static void saveWorking(JSONObject jObj)
    {
        try {
            LocalStorage.save("Workings", Utils.getString(jObj, "Id"), jObj);
        }
        catch (Exception E) { }
        loadWorkings();
    }

    public static void removeWorking(String Id)
    {
        try {
            LocalStorage.remove("Workings", Id);
        }
        catch (Exception E) { }
        loadWorkings();
    }

    public static void saveWorkings(JSONArray data)
    {
        try {
            for (int i = 0; i < data.length(); i++) {
                JSONObject jObj = data.getJSONObject(i);
                LocalStorage.save("Workings", jObj.getString("Id"), jObj);
            }
        }
        catch (Exception E) { }
        loadWorkings();
    }

    public static void loadWorkings()
    {
        SharedPreferences prefs = LocalStorage.getPrefs("Workings");
        JSONArray data = new JSONArray();
        try {
            Map<String, ?> allEntries = prefs.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                JSONObject jObj = new JSONObject(entry.getValue().toString());
                data.put(jObj);
            }
        }
        catch (Exception E) { }
        Workings.clear();
        Workings = Working.Parse(data);
    }

    public static Working getWorking(String Id)
    {
        for (int i = 0; i < Workings.size(); i++)
        {
            if (Workings.get(i).getId().equals(Id))
                return Workings.get(i);
        }
        return null;
    }

}
