package com.utopiaindustries.qualitychecker.store;

import android.content.Context;

import com.utopiaindustries.qualitychecker.apiservice.ApiService;
import com.utopiaindustries.qualitychecker.apiservice.ApiServiceFactory;
import com.utopiaindustries.qualitychecker.db.CheckpointRepository;
import com.utopiaindustries.qualitychecker.db.DefectRepository;
import com.utopiaindustries.qualitychecker.db.ItemRepository;
import com.utopiaindustries.qualitychecker.models.EmployeePhoto;
import com.utopiaindustries.qualitychecker.models.InspectionCheckPoint;
import com.utopiaindustries.qualitychecker.models.InspectionCheckpointSku;
import com.utopiaindustries.qualitychecker.models.InspectionDefect;
import com.utopiaindustries.qualitychecker.models.InspectionDimension;
import com.utopiaindustries.qualitychecker.models.InspectionItemCheckPoint;
import com.utopiaindustries.qualitychecker.models.InspectionItemDimension;
import com.utopiaindustries.qualitychecker.models.InspectionReport;
import com.utopiaindustries.qualitychecker.models.InspectionReportItem;
import com.utopiaindustries.qualitychecker.models.ItemUnit;
import com.utopiaindustries.qualitychecker.models.Product;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Store {

    private static Store instance;
    private InspectionReport report;
    private ApiService apiService;
    private List<InspectionCheckPoint> checkPoints;
    private List<InspectionDefect> defects;
    private List<InspectionDimension> dimensionList;
    private List<InspectionItemDimension> dimensions;
    private List<Product> products;
    private EmployeePhoto employeePhoto;
    private List<ItemUnit> itemUnits;
    private List<InspectionCheckpointSku> checkpointSkus;
    private boolean isNewReportPresented;
    private boolean isNewReportSelected;
    private boolean isNewAQL;

    private Store(){
    }

    public static synchronized Store getInstance(){
        if( instance == null ){
            instance = new Store();
        }
        return instance;
    }

    public synchronized void initializeNewReport( Context context ) {
        checkPoints = new ArrayList<>();
        defects = new ArrayList<>();
        dimensions = new ArrayList<>();
        products = new ArrayList<>();
        itemUnits = new ArrayList<>();
        dimensionList = new ArrayList<>();
        apiService = ApiServiceFactory.getApiService();
        report = new InspectionReport();
        InspectionReportItem item = new InspectionReportItem();
        item.setDimensions( new ArrayList<>() );
        report.setItems(Collections.singletonList( item ));

//        apiService.fetchCheckPoints().enqueue(
//                new Callback<List<InspectionCheckPoint>>() {
//                    @Override
//                    public void onResponse( Call<List<InspectionCheckPoint>> call,
//                                            Response<List<InspectionCheckPoint>> response) {
//                        if( response.isSuccessful() && response.body() != null ){
//                            checkPoints =  response.body() ;
//                        }
//                    }
//                    @Override
//                    public void onFailure(Call<List<InspectionCheckPoint>> call, Throwable t) {
//                        System.out.println( t.getMessage() );
//                    }
//                }
//        );

        CheckpointRepository checkpointRepository = new CheckpointRepository( context );
        checkPoints  = checkpointRepository.findAll();

//        apiService.fetchDefects().enqueue(
//                new Callback<List<InspectionDefect>>() {
//                    @Override
//                    public void onResponse( Call<List<InspectionDefect>> call,
//                                            Response<List<InspectionDefect>> response) {
//                        if( response.isSuccessful() && response.body() != null ){
//                            defects = response.body();
//                        }
//                    }
//                    @Override
//                    public void onFailure( Call<List<InspectionDefect>> call, Throwable t) {
//                        System.out.println( t.getMessage() );
//                    }
//                }
//        );
        DefectRepository defectRepository = new DefectRepository( context );
        defects = defectRepository.findAll();

        List<InspectionItemCheckPoint> itemCp = new ArrayList<>();
            for( InspectionCheckPoint cp : checkPoints ){
                InspectionItemCheckPoint icp = new InspectionItemCheckPoint();
                icp.setTitle( cp.getTitle() );
                icp.setChecked( false );
                icp.setRemarks("");
                icp.setDefectTitles( new ArrayList<>() );
                icp.setDefectSeverites( new ArrayList<>() );
                icp.setQuantities( new ArrayList<>() );
                itemCp.add( icp );
            }
            report.getItems().get( 0 ).setCheckPoints( itemCp );



//        apiService.fetchCheckPoints().enqueue(
//                new Callback<List<InspectionCheckPoint>>() {
//                    @Override
//                    public void onResponse(Call<List<InspectionCheckPoint>> call,
//                                           Response<List<InspectionCheckPoint>> response) {
//
//                        if( response.isSuccessful() && response.body() != null ){
//                            List<InspectionItemCheckPoint> itemCp = new ArrayList<>();
//                            for( InspectionCheckPoint cp : response.body() ){
//                                InspectionItemCheckPoint icp = new InspectionItemCheckPoint();
//                                icp.setTitle( cp.getTitle() );
//                                icp.setChecked( false );
//                                icp.setRemarks("");
//                                icp.setDefectTitles( new ArrayList<>() );
//                                icp.setDefectSeverites( new ArrayList<>() );
//                                icp.setQuantities( new ArrayList<>() );
//                                itemCp.add( icp );
//                            }
//                            report.getItems().get( 0 ).setCheckPoints( itemCp );
//                        }
//                    }
//                    @Override
//                    public void onFailure(Call<List<InspectionCheckPoint>> call, Throwable t) {
//                        System.out.println( t.getMessage() );
//                    }
//                }
//        );

//        apiService.fetchItemUnits().enqueue(
//                new Callback<List<ItemUnit>>() {
//                    @Override
//                    public void onResponse(Call<List<ItemUnit>> call,
//                                           Response<List<ItemUnit>> response) {
//                        if( response.isSuccessful() && response.body() != null ){
//                            System.out.println( response.body() );
//                            itemUnits = response.body();
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(Call<List<ItemUnit>> call,
//                                          Throwable t) {
//                        System.out.println( t.getMessage() );
//                    }
//                }
//        );
        ItemRepository itemRepository = new ItemRepository( context );
        itemUnits = itemRepository.findAll();
    }

    public InspectionReport getReport() {
        return report;
    }

    public void setReport(InspectionReport report) {
        this.report = report;
    }

    public List<InspectionCheckPoint> getCheckPoints() {
        return checkPoints;
    }

    public List<InspectionDefect> getDefects() {
        return defects;
    }

    public List<InspectionItemDimension> getDimensions() {
        return dimensions;
    }

    public void setDimensions(List<InspectionItemDimension> dimensions) {
        this.dimensions = dimensions;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public EmployeePhoto getEmployeePhoto() {
        return employeePhoto;
    }

    public void setEmployeePhoto(EmployeePhoto employeePhoto) {
        this.employeePhoto = employeePhoto;
    }

    public List<String> getItemUnits() {
        return itemUnits.stream().map( ItemUnit::getTitle ).collect(Collectors.toList());
    }

    public void setItemUnits(List<ItemUnit> itemUnits) {
        this.itemUnits = itemUnits;
    }

    public String getProfileImage(  ) {
        if( employeePhoto != null  ){
            return "https://portal.utopiaindustries.pk/uind/employee-photo/" + employeePhoto.getEmployeeId() +".jpeg";
        }
        return "";
    }

    public List<String> getDimensionList() {
        return dimensionList.stream().map( InspectionDimension::getTitle ).collect(Collectors.toList());
    }

    public void setDimensionList(List<InspectionDimension> dimensionList) {
        this.dimensionList = dimensionList;
    }

    public List<InspectionCheckpointSku> getCheckpointSkus() {
        return checkpointSkus;
    }

    public void setCheckpointSkus(List<InspectionCheckpointSku> checkpointSkus) {
        this.checkpointSkus = checkpointSkus;
    }

    public boolean isNewReportPresented() {
        return isNewReportPresented;
    }

    public void setNewReportPresented(boolean newReportPresented) {
        isNewReportPresented = newReportPresented;
    }

    public boolean isNewReportSelected() {
        return isNewReportSelected;
    }

    public void setNewReportSelected(boolean newReportSelected) {
        isNewReportSelected = newReportSelected;
    }

    public boolean isNewAQL() {
        return isNewAQL;
    }

    public void setNewAQL(boolean newAQL) {
        isNewAQL = newAQL;
    }

    @Override
    public String toString() {
        return "Store{" +
                "report=" + report +
                ", apiService=" + apiService +
                ", checkPoints=" + checkPoints +
                ", defects=" + defects +
                '}';
    }
}
