package in.techware.carrefour.util.menuUtils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;

import in.techware.carrefour.model.DepartmentBean;
import in.techware.carrefour.model.OriginBean;
import in.techware.carrefour.model.PrinterBean;
import in.techware.carrefour.model.SectionBean;
import in.techware.carrefour.model.UserBean;
import in.techware.carrefour.model.UserListBean;
import in.techware.carrefour.util.FileOp;

/**
 * Created by Jemsheer K D on 29 July, 2018.
 * Package in.techware.carrefour.util.menuUtils
 * Project Carrefour
 */
public class DropDownMenuUtil {

    public static ArrayList<OriginBean> getOriginList() {

        ArrayList<OriginBean> menuList = new ArrayList<>();

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "origin");
        String[] stringList = TextUtils.split(strFile, "\n");

        int count = 0;
        for (String str : stringList) {
            OriginBean originBean = new OriginBean();
            originBean.setOrigin(str);
            originBean.setOriginCode(str.length() >= 2 ? str.substring(0, 2) : "");
            originBean.setOriginName(str.length() > 2 ? str.substring(2) : "");
            originBean.setPosition(count);
            menuList.add(originBean);
            count++;
        }
        return menuList;
    }

    public static UserListBean getUserList() {

        UserListBean userListBean = new UserListBean();

        ArrayList<UserBean> menuList = new ArrayList<>();

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "User");
        String[] stringList = TextUtils.split(strFile, "\n");

        for (String str : stringList) {
            UserBean userBean = new UserBean();
            userBean.setUsername(str.length() >= 3 ? str.substring(0, 3) : "");
            userBean.setPassword(str.length() >= 6 ? str.substring(3, 6) : "");
            userBean.setAdminStatus(str.length() >= 7 ? str.substring(7, 7) : "");
            menuList.add(userBean);
        }

        userListBean.setUserList(menuList);
        
        return userListBean;
    }

    public static ArrayList<DepartmentBean> getDepartmentList() {

        ArrayList<DepartmentBean> menuList = new ArrayList<>();

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "dept");
        String[] stringList = TextUtils.split(strFile, "\n");

        int count = 0;
        for (String str : stringList) {
            DepartmentBean departmentBean = new DepartmentBean();
            departmentBean.setDepartment(str);
            departmentBean.setDepartmentCode(str.length() >= 2 ? str.substring(0, 2) : "");
            departmentBean.setDepartmentName(str.length() > 2 ? str.substring(2) : "");
            departmentBean.setPosition(count);
            menuList.add(departmentBean);
            count++;
        }
        return menuList;
    }

    public static ArrayList<SectionBean> getSectionList() {

        ArrayList<SectionBean> menuList = new ArrayList<>();

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "section");
        String[] stringList = TextUtils.split(strFile, "\n");

        int count = 0;
        for (String str : stringList) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setSection(str);
            sectionBean.setSectionCode(str.length() >= 2 ? str.substring(0, 2) : "");
            sectionBean.setSectionSecCode(str.length() >= 5 ? str.substring(2, 5) : "");
            sectionBean.setSectionName(str.length() > 5 ? str.substring(5) : "");
            sectionBean.setPosition(count);
            menuList.add(sectionBean);
            count++;
        }
        return menuList;
    }

    public static ArrayList<SectionBean> getSectionList(String departmentCode) {

        ArrayList<SectionBean> menuList = new ArrayList<>();

        ArrayList<SectionBean> sectionList = getSectionList();

        for (SectionBean bean : sectionList) {
            if (bean.getSectionCode().equals(departmentCode)) {
                menuList.add(bean);
            }
        }
        return menuList;
    }

    public static ArrayList<String> getGondolaList() {

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "Gondola");
        String[] stringList = TextUtils.split(strFile, "\n");
        return new ArrayList<>(Arrays.asList(stringList));
    }

    public static ArrayList<PrinterBean> getPrinterList() {
        ArrayList<PrinterBean> menuList = new ArrayList<>();

        FileOp fop = new FileOp();
        String strFile = fop.readExternalFile(FileOp.RECEIVED_FILES + FileOp.SEPARATOR + "Printerlist");
        String[] stringList = TextUtils.split(strFile, "\n");

        for (String str : stringList) {
            PrinterBean printerBean = new PrinterBean();
            String[] values = TextUtils.split(str, " ");
            if (values.length == 3) {
                printerBean.setId(values[0]);
                printerBean.setName(values[1]);
                printerBean.setLabel(values[2]);
                menuList.add(printerBean);
            }
        }
        menuList.add(0, new PrinterBean());
        return menuList;
    }

}
