package com.xixun.xixunplayer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.Choreographer;
import android.view.View;
import android.webkit.WebView;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

import gnph.util.JSList;
import gnph.util.JSMap;
import gnph.util.NumFmts;

public class SrcSensor extends WebView implements IntentReceiver {

    static String directs[] = {"NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N"};
    MainActivity act;
    String html, lineHeight, prefix;
    int interval, cur, end, step;
    boolean isFirst = true;

    public SrcSensor(Context context, JSMap json) {
        super(context);
        act = (MainActivity) context;
        setBackgroundColor(Color.TRANSPARENT);
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        setInitialScale(100);
        html = json.stnn("html");
        lineHeight = json.str("lineHeight");
        prefix = "<body style=\"color:#fff;margin:0;padding:0;";
        if(lineHeight!=null) prefix += "line-height:"+lineHeight+";";
        prefix += "\">";
    }

    public static Method method;
    static {
        try {
            method = Intent.class.getMethod("getExtra", String.class, Object.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void onReceive(Intent intent) {
        try {
//            for(var item : items) {
//                item.nums.clear();
//                if(item.unit==null) {
//                    var num = intent.getIntExtra(item.key, -1);
//                    if(num>=0 && num<=15) item.nums.add(imgMap.get(directs[num]));
//                    else {
//                        var img = imgMap.get("-");
//                        item.nums.add(img);
//                        item.nums.add(img);
//                    }
//                } else {
//                    var num = ((Number) method.invoke(intent, item.key, -999)).doubleValue();
//                    var str = num==-999 || (num==-1 && ! item.key.endsWith("rature")) ? "--" : NumFmts.zz().format(num);
//                    for(int cc=0; cc<str.length(); cc++) item.nums.add(imgMap.get(str.substring(cc, cc+1)));
//                }
//            }
            var htm = html;
            loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null);
        } catch (Exception e) {
            Util.printStackTrace(e);
        }
    }

    @Override
    public void onVisibilityAggregated(boolean isVisible) {
        super.onVisibilityAggregated(isVisible);
        if(isVisible) {
            if(isFirst) {
                isFirst = false;
                onReceive(act.environIntent);
                act.environs.add(this);
            }
        } else {
            act.environs.remove(this);
            isFirst = true;
        }
    }
}
