package cn.zhios.zhiying_comm;


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

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

public class ZhiyingFlutterCommNative implements MethodCallHandler {

    private ZhiyingFlutterCommNativable       nat;
    private List<ZhiyingFlutterCommNativable> natLists;

    /**
     * 类级的内部类，也就是静态的成员式内部类，该内部类的实例与外部类的实例 没有绑定关系，而且只有被调用到才会装载，从而实现了延迟加载
     */
    private static class HairuyiFlutterCommNativeHolder {
        /**
         * 静态初始化器，由JVM来保证线程安全
         */
        private static ZhiyingFlutterCommNative instance = new ZhiyingFlutterCommNative();
    }

    private ZhiyingFlutterCommNative() {
        natLists = new ArrayList<>();
    }

    public static ZhiyingFlutterCommNative getInstance() {
        return HairuyiFlutterCommNativeHolder.instance;
    }

    public void registerWith( BinaryMessenger message) {
        MethodChannel channel = new MethodChannel(message, "zhiying_comm://method");
        channel.setMethodCallHandler(ZhiyingFlutterCommNative.getInstance());
    }

    public void regist(ZhiyingFlutterCommNativable nat) {
        if (null == natLists){ natLists = new ArrayList<>();}
        natLists.add(nat);
        this.nat = natLists.get(natLists.size() - 1); // 获取新的
    }

    public void unregist(){
        if (null != natLists && natLists.size() > 1){
            natLists.remove(natLists.size() - 1);
            nat = natLists.get(natLists.size() -1);
        }else if (null != natLists && natLists.size() == 1){
            natLists.clear();
            nat = null;
        }
    }

    @Override
    public void onMethodCall(MethodCall call, final Result result) {

        /* 公共跳转方法 */
        if (call.method.equals("openPage")) {
            if (nat != null) {
                nat.openPage((Map) call.arguments);
            }
            Map map = new HashMap<String, Object>();
            map.put("success", "1");
            result.success(map);
            return;
        }

        /* 跳转原生页面（非公共跳转） */
        if (call.method.equals("openNativePage")) {
            if (nat != null) {
                nat.openNativePage((Map) call.arguments);
            }
            Map map = new HashMap<String, Object>();
            map.put("success", "1");
            result.success(map);
            return;
        }

        /* 获取设置 */
        if (call.method.equals("getSetting")) {
            Map map = new HashMap<String, Object>();
            if (nat != null) {
                map = nat.getSetting();
            }
            result.success(map);
            return;
        }

        /* 调用原生方法 */
        if (call.method.equals("invokeMethod")) {
            if (nat != null) {
                Map params = ((Map<String, String>) call.arguments);
                nat.invokeMethod((String) params.get("method"), (Map) params.get("params"), new ZhiyingFlutterCommHandler() {
                    @Override
                    public void handle(ZhiyingFlutterCommNativeResult res) {
                        result.success(res.toMap());
                    }
                });
            } else {
                result.success(ZhiyingFlutterCommNativeResult.notImp().toMap());
                return;
            }

        } else {

            result.notImplemented();
            return;
        }

    }
}
