package com.philisense.rdgo.activity;

import android.widget.EditText;

import com.philisense.rdgo.BuildConfig;
import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.response.BoxInfoBean;
import com.philisense.rdgo.bean.response.OrderDetailBean;
import com.philisense.rdgo.ui.putOut.PutOutOrderActivity;
import com.philisense.rdgo.viewModel.PutOutOrderViewModel;
import com.unnamed.b.atv.model.TreeNode;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.internal.RealmCore;
import io.realm.log.RealmLog;

import static junit.framework.Assert.assertEquals;

/**
 * Created by shaop on 2017/12/18.
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest({Realm.class, RealmConfiguration.class, RealmQuery.class, RealmResults.class, RealmCore.class, RealmLog.class})
public class PutOutOrderActivityTest {

    private PutOutOrderActivity activity;
    private PutOutOrderViewModel viewModel;

    @Before
    public void setUp() {
        activity = Robolectric.setupActivity(PutOutOrderActivity.class);
        activity.setContentView(R.layout.activity_put_out_order);

        activity.oilMap = new HashMap<>();
        activity.nodeMap = new HashMap<>();

        viewModel = new PutOutOrderViewModel(activity);

        List<OrderDetailBean.DataBean.ProductBean> list = new ArrayList<>();
        OrderDetailBean.DataBean.ProductBean bean = new OrderDetailBean.DataBean.ProductBean();
        bean.set_id("0000001");
        bean.setOrderId("o00000001");
        bean.setProductCode("p0000001");
        bean.setOrderTotal(40);

        OrderDetailBean.DataBean.ProductBean bean2 = new OrderDetailBean.DataBean.ProductBean();
        bean2.set_id("0000002");
        bean2.setOrderId("o00000002");
        bean2.setProductCode("p0000002");
        bean2.setOrderTotal(30);

        OrderDetailBean.DataBean.ProductBean bean3 = new OrderDetailBean.DataBean.ProductBean();
        bean3.set_id("0000003");
        bean3.setOrderId("o00000003");
        bean3.setProductCode("p0000002");
        bean3.setOrderTotal(40);

        list.add(bean);
        list.add(bean2);
        list.add(bean3);

        activity.initNodeView(list);

        assertEquals(activity.nodeMap.size(), 2);
        assertEquals(activity.oilMap.size(), 2);
    }

    @Test
    public void testBoxBigger() {
        BoxInfoBean boxInfoBean = new BoxInfoBean();
        BoxInfoBean.DataBean bean = new BoxInfoBean.DataBean();
        bean.setPileBoxNumber(31);
        bean.setPileId("pile000001");
        bean.setTypeCode("p0000002");
        boxInfoBean.setData(bean);
        activity.checkPile(boxInfoBean,"b0000001");

        TreeNode node = activity.nodeMap.get("p0000002o00000002");
        assertEquals(node.getChildren().size(),1);
    }

    @Test
    public void testBoxSmaller() {
        BoxInfoBean boxInfoBean = new BoxInfoBean();
        BoxInfoBean.DataBean bean = new BoxInfoBean.DataBean();
        bean.setPileBoxNumber(20);
        bean.setPileId("pile000001");
        bean.setTypeCode("p0000002");
        boxInfoBean.setData(bean);
        activity.checkPile(boxInfoBean,"b0000001");

        TreeNode node = activity.nodeMap.get("p0000002o00000002");
        assertEquals(node.getChildren().size(),0);
    }


    @Test
    public void testBoxIn() {
        EditText editText = new EditText(activity);
        editText.setText("");
        activity.onClickBoxInput(editText,null);
        assertEquals(ShadowToast.getTextOfLatestToast(),"输入了非法数值");
    }


//    @Test
//    public void testAddPile() {
//        // case 1 先添加30箱
//        PileInfoBean bean = new PileInfoBean();
//        PileInfoBean.DataBean dataBean = new PileInfoBean.DataBean();
//        dataBean.setTypeCode("p0000002");
//        ArrayList<PileInfoBean.DataBean> list = new ArrayList<>();
//        for (int i = 0; i < 29; i++) {
//            PileInfoBean.DataBean boxesBean = new PileInfoBean.DataBean();
//            boxesBean.setBoxIn();
//            boxesBean.setBoxId("b000000" + i);
//            list.add(boxesBean);
//        }
//        dataBean.setBoxes(list);
//        bean.setData(dataBean);
//
//        activity.loadAllBoxes(bean,"pile000001");
//
//        TreeNode node = activity.nodeMap.get("p0000002o00000002");
//        assertEquals(node.getChildren().size(), 29);
//
//        // case 2 再添加20箱，
//        PileInfoBean bean2 = new PileInfoBean();
//        PileInfoBean.DataBean dataBean2 = new PileInfoBean.DataBean();
//        dataBean2.setTypeCode("p0000002");
//        ArrayList<PileInfoBean.DataBean.BoxesBean> list2 = new ArrayList<>();
//        for (int i = 20; i < 40; i++) {
//            PileInfoBean.DataBean.BoxesBean boxesBean = new PileInfoBean.DataBean.BoxesBean();
//            boxesBean.setBoxId("b000000" + i);
//            list2.add(boxesBean);
//        }
//        dataBean2.setBoxes(list2);
//        bean2.setData(dataBean2);
//
//        activity.loadAllBoxes(bean2,"pile000002");
//
//        TreeNode node2 = activity.nodeMap.get("p0000002o00000003");
//        assertEquals(node.getChildren().size(), 30);
//        assertEquals(node2.getChildren().size(), 10);
//
//    }
//
//    @Test
//    public void testRespondsData() {
//        // case 0 添加40箱到第一个
//        PileInfoBean bean0 = new PileInfoBean();
//        PileInfoBean.DataBean dataBean0 = new PileInfoBean.DataBean();
//        dataBean0.setTypeCode("p0000001");
//        ArrayList<PileInfoBean.DataBean > list0 = new ArrayList<>();
//        for (int i = 0; i < 40; i++) {
//            PileInfoBean.DataBean.BoxesBean boxesBean = new PileInfoBean.DataBean.BoxesBean();
//            boxesBean.setBoxId("b001111" + i);
//            list0.add(boxesBean);
//        }
//        dataBean0.setBoxes(list0);
//        bean0.setData(dataBean0);
//
//        activity.loadAllBoxes(bean0,"pile000000");
//
//        TreeNode node0 = activity.nodeMap.get("p0000001o00000001");
//        assertEquals(node0.getChildren().size(), 40);
//
//        // case 1 先添加30箱
//        PileInfoBean bean = new PileInfoBean();
//        PileInfoBean.DataBean dataBean = new PileInfoBean.DataBean();
//        dataBean.setTypeCode("p0000002");
//        ArrayList<PileInfoBean.DataBean.BoxesBean> list = new ArrayList<>();
//        for (int i = 0; i < 29; i++) {
//            PileInfoBean.DataBean.BoxesBean boxesBean = new PileInfoBean.DataBean.BoxesBean();
//            boxesBean.setBoxId("b000000" + i);
//            list.add(boxesBean);
//        }
//        dataBean.setBoxes(list);
//        bean.setData(dataBean);
//
//        activity.loadAllBoxes(bean,"pile000001");
//
//        TreeNode node = activity.nodeMap.get("p0000002o00000002");
//        assertEquals(node.getChildren().size(), 29);
//
//        // case 2 再添加20箱，
//        PileInfoBean bean2 = new PileInfoBean();
//        PileInfoBean.DataBean dataBean2 = new PileInfoBean.DataBean();
//        dataBean2.setTypeCode("p0000002");
//        ArrayList<PileInfoBean.DataBean.BoxesBean> list2 = new ArrayList<>();
//        for (int i = 20; i < 70; i++) {
//            PileInfoBean.DataBean.BoxesBean boxesBean = new PileInfoBean.DataBean.BoxesBean();
//            boxesBean.setBoxId("b000000" + i);
//            list2.add(boxesBean);
//        }
//        dataBean2.setBoxes(list2);
//        bean2.setData(dataBean2);
//
//        activity.loadAllBoxes(bean2,"pile000002");
//
//        TreeNode node2 = activity.nodeMap.get("p0000002o00000003");
//        assertEquals(node.getChildren().size(), 30);
//        assertEquals(node2.getChildren().size(), 40);
//
//
//        viewModel.submitOrder(activity.nodeMap, new RequestImpl<String>() {
//            @Override
//            public void loadSuccess(String bean) {
//                Log.e("tag",bean);
//            }
//            @Override
//            public void loadFailed(OError error) {
//
//            }
//        });
//    }

}
