package com.bpa.scalage.Server;

import android.util.Log;
import android.view.View;

import com.bpa.scalage.HBL.API.APIHelper;
import com.bpa.scalage.HBL.API.APIResult;
import com.bpa.scalage.HBL.API.APIResultT;
import com.bpa.scalage.HBL.DataUtil.Convert;
import com.bpa.scalage.HBL.Interface.IRun;
import com.bpa.scalage.HBL.Interface.IRunT;
import com.bpa.scalage.HBL.Logs.MessageLog;
import com.bpa.scalage.MainActivity;
import com.bpa.scalage.MenuSelectionActivity;
import com.bpa.scalage.R;
import com.bpa.scalage.app.Main;
import com.bpa.scalage.config.ConfigName;
import com.bpa.scalage.control.ConsumedAdapter;
import com.bpa.scalage.control.MyAdapter;
import com.bpa.scalage.data.DataBus;
import com.bpa.scalage.helper.Json;
import com.bpa.scalage.helper.SortTools;
import com.bpa.scalage.helper.T;
import com.bpa.scalage.mode.Foods.FoodTypes;
import com.bpa.scalage.mode.Order.BPA_Order;
import com.bpa.scalage.mode.Order.BPA_SubOrder;
import com.bpa.scalage.mode.apimode.ReceiveUserAndCartGXModel;
import com.google.gson.Gson;

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

public class APIServer {

    private static String address="https://groupmeal.black-pa.com/";

//    private static String address="https://bpa.black-pa.com:21995/";

    /**
     * 获取菜品信息
     */
    public static void GetDishInformation(IRunT<List<String>> callback,IRunT<String> faultCallback){
        String url =address+ "saasbase/api/ExternalPlatform/WeighingService/GetGoodsInfo";
        String key = "a78bb2e8-7007-447b-b378-7ec4e07ec3ee";

        APIHelper.Post(url,new Object(),key,(s)->{
            MessageLog.ShowInfo(s.toString());
            if(s.errors!=null&&!s.errors.isEmpty()){
                if(faultCallback!=null)faultCallback.Run(s.errors);
            }
            Convert.fromJson(s.data, FoodTypes[].class).OnSource(res->{
                DataBus.getInstance().foodTypes= Arrays.asList(res.Content);
                List<String> types_key=new ArrayList<>();
                DataBus.getInstance().foodTypes.forEach(item->{types_key.add(item.name);});
                if(callback!=null)callback.Run(types_key);
            });
        } );
    }

    /**
     * 获取RFID和订单的信息
     */
    public static void GetOrderInfo(String ChipCode,IRunT<WeighOrderDto> callback,IRunT<String> faultCallback){
        String url = address+ "order/api/ExternalPlatform/WeighOrder/GetWeighOrderByChipCode";
        String key = "26dc841d-d945-4e1e-968b-cc34a85cedb9";
        ChipCodeRequiredModel crm = new ChipCodeRequiredModel(ChipCode);

        APIHelper.Post(url,crm,key,(s)->{
            MessageLog.ShowInfo(s.toString());
            if(s.errors!=null&&!s.errors.isEmpty()){
                if(faultCallback!=null)faultCallback.Run(s.errors);
            }
            Convert.fromJson(s.data, WeighOrderDto.class).OnSource(res->{
                MessageLog.ShowInfo("解析数据成功");
                DataBus.getInstance().weighOrderDto=res.Content;
                if(callback!=null)callback.Run(res.Content);
            });
        } );
    }

    /**
     * 添加商品子订单
     */
    public static void CreateOrder(AddGoodInfo agi, IRun callback,IRunT<String> faultCallback){
        String url =address+  "order/api/ExternalPlatform/WeighOrder/AddWeighOrderGoods";
        String key = "26dc841d-d945-4e1e-968b-cc34a85cedb9";
        AddGoodModel agm = new AddGoodModel();
        agm.data.add(agi);
        APIHelper.Post(url,agm,key,(s)->{
            MessageLog.ShowInfo(s.toString());
            if(s.errors!=null&&!s.errors.isEmpty()){
                if(faultCallback!=null)faultCallback.Run(s.errors);
            }
            if(callback!=null)callback.Run();
        } );
    }
}
