package com.gingersoft.gsa.cloud.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.CCUtil;
import com.billy.cc.core.component.IComponent;
import com.gingersoft.gsa.cloud.common.logan.LoganManager;
import com.gingersoft.gsa.cloud.component.ComponentAction;
import com.gingersoft.gsa.cloud.component.ComponentName;
import com.gingersoft.gsa.cloud.common.function.FModule;
import com.gingersoft.gsa.cloud.main.mvp.ui.activity.MainActivity;
import com.gingersoft.gsa.cloud.main.mvp.ui.activity.NewMainActivity;
import com.gingersoft.gsa.cloud.main.mvp.ui.activity.SettlementReportActivity;
import com.gingersoft.gsa.cloud.main.mvp.ui.activity.menu.FoodMenuManageActivity;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;

public class ComponentMain implements IComponent {

    private final String TAG = getClass().getSimpleName();

    public static final class main {
        /**
         * 首頁- 點餐
         */
        public static final FModule[] order = {
                new FModule("main/order", 0, 0),
                new FModule("main/order/table", R.drawable.ic_dining_table_mode, R.drawable.ic_dining_table_mode_close),
                new FModule("main/order/delivery", R.drawable.ic_delivery_mode, R.drawable.ic_delivery_mode_close),
                new FModule("main/order/takeaway", R.drawable.ic_outsourcing_model, R.drawable.ic_outsourcing_model_close),
                new FModule("main/order/preorder", R.drawable.ic_pre_order_mode, R.drawable.ic_pre_order_mode_close),
                new FModule("main/order/coldChain", R.drawable.ic_cold_chain, R.drawable.ic_pre_order_mode_close),
        };
        /**
         * 首頁- 管理
         */
        public static final FModule[] manager = {
                new FModule("main/manager", 0, 0),
                new FModule("main/manager/takeaway", R.drawable.ic_meals_menu_management, R.drawable.ic_meals_menu_management_close),
                new FModule("main/manager/bill", R.drawable.ic_meals_menu_management, R.drawable.ic_meals_menu_management_close),
                new FModule("main/manager/table", R.drawable.ic_dining_table_management, R.drawable.ic_dining_table_management_close),
                new FModule("main/manager/printer", R.drawable.ic_print_management, R.drawable.ic_print_management_close),
                new FModule("main/manager/printQuery", R.drawable.ic_kitchen_print_query, R.drawable.ic_kitchen_print_query),
                new FModule("main/manager/pay", R.drawable.ic_pay_management, R.drawable.ic_pay_management_close),
                new FModule("main/manager/discout", R.drawable.ic_discount_management, R.drawable.ic_discount_management_close),
                new FModule("main/manager/soldoutCtr", R.drawable.ic_sell_off_manger, R.drawable.ic_sell_off_manger),
                new FModule("main/manager/qrCode", R.drawable.ic_qr_code, R.drawable.ic_qr_code),
                new FModule("main/manager/purchase", R.drawable.ic_purchase, R.drawable.ic_purchase)
        };
        /**
         * 首頁- 員工管理
         */
        public static final FModule[] employee = {
                new FModule("main/employee", 0, 0),
                new FModule("main/employee/management", R.drawable.ic_staff_management_close, R.drawable.ic_staff_management_close),
                new FModule("main/employee/delivery", R.drawable.ic_authority_management_close, R.drawable.ic_authority_management_close),
                new FModule("main/employee/operationRecord", R.drawable.ic_operation_record_close, R.drawable.ic_operation_record_close)
        };
    }

    @Override
    public String getName() {
        //组件的名称，调用此组件的方式：
        // CC.obtainBuilder("ComponentA")...build().callAsync()
        return ComponentName.COMPONENT_MAIN ;
    }

    /**
     * 组件被调用时的入口
     * 要确保每个逻辑分支都会调用到CC.sendCCResult，
     * 包括try-catch,if-else,switch-case-default,startActivity
     *
     * @param cc 组件调用对象，可从此对象中获取相关信息
     * @return true:将异步调用CC.sendCCResult(...),用于异步实现相关功能，例如：文件加载、网络请求等
     * false:会同步调用CC.sendCCResult(...),即在onCall方法return之前调用，否则将被视为不合法的实现
     */
    @Override
    public boolean onCall(CC cc) {
        Context context = cc.getContext();
        String actionName = cc.getActionName();
        switch (actionName) {
            case "showMainActivity":
                LoganManager.w_code(TAG,"onCall showMainActivity");
                openActivity(cc);
                break;
            case "foodMenuActivity":
                Intent intent = new Intent(cc.getContext(), FoodMenuManageActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                cc.getContext().startActivity(intent);
                break;
            case "getLifecycleFragment":
                //demo for provide fragment object to other component
                getLifecycleFragment(cc);
                break;
            case "lifecycleFragment.addText":
                lifecycleFragmentDoubleText(cc);
                break;
            case "getInfo":
                getInfo(cc);
                break;
            default:
                //这个逻辑分支上没有调用CC.sendCCResult(...),是一种错误的示例
                //并且方法的返回值为false，代表不会异步调用CC.sendCCResult(...)
                //在LocalCCInterceptor中将会返回错误码为-10的CCResult
                break;
        }
        return false;
    }

    private void lifecycleFragmentDoubleText(CC cc) {
//        LifecycleFragment lifecycleFragment = cc.getParamItem("fragment");
//        if (lifecycleFragment != null) {
//            String text = cc.getParamItem("text", "");
//            lifecycleFragment.addText(text);
//            CC.sendCCResult(cc.getCallId(), CCResult.success());
//        } else {
//            CC.sendCCResult(cc.getCallId(), CCResult.error("no fragment params"));
//        }
    }

    private void getLifecycleFragment(CC cc) {
//        CC.sendCCResult(cc.getCallId(), CCResult.successWithNoKey(new LifecycleFragment()));
    }

    private void getInfo(CC cc) {
        String userName = "billy";
        CC.sendCCResult(cc.getCallId(), CCResult.success("userName", userName));
    }

    private void openActivity(CC cc) {
        Intent intent = new Intent(cc.getContext(), NewMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        cc.getContext().startActivity(intent);
    }

}
