package basic.taobaotv.view;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;

import com.wasu.module.log.WLog;
import com.wasu.util.NetUtils;

import basic.taobaotv.TaobaoTvManager;
import basic.taobaotv.present.WasuTaobaoTVClient;
import cn.com.wasu.main.R;

/**
 * Created by fanwang on 10/8/16.
 */
public class ActivityTaobaoTv extends Activity implements ITaobaoTvView {
    private String TAG = "ActivityTaobaoTv";

    WebView mWebView = null;
    WasuTaobaoTVClient tvClient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WLog.i(TAG,"onCreate()");

        this.setContentView(R.layout.activity_taobao_tv);
        mWebView = (WebView) this.findViewById(R.id.taobao_tv_webview);
        tvClient = new WasuTaobaoTVClient(this);
        mWebView.setWebViewClient(tvClient);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mWebView.getSettings().setLoadWithOverviewMode(false);
        mWebView.setInitialScale((int) (dm.widthPixels / 1280f * 100));
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        //open js
        mWebView.getSettings().setJavaScriptEnabled(true);
        //close focuse
        mWebView.setFocusable(false);

        String mac = NetUtils.getMacAddress(this);
        String taobaoTvUrl = TaobaoTvManager.getInstance().getTaobaoTvUrl(mac, mac);
        mWebView.loadUrl(taobaoTvUrl);
    }

    @Override
    public void onBackUrl() {
        //clear
        mWebView = null;
        tvClient = null;

        this.finish();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if (mWebView.getVisibility() == View.VISIBLE || !(mWebView.getUrl().equals(""))) {
                    String str = "javascript:(function(){"
                            + "var customEvent = document.createEvent(\"HTMLEvents\");"
                            + "customEvent.initEvent(\"keydown\", true, true);"
                            + "customEvent.eventName = \"keydown\";"
                            + "customEvent.keyCode = customEvent.which = 8;"
                            + "document.dispatchEvent(customEvent);" + "})();";
                    WLog.i("WANGFAN", str);
                    mWebView.loadUrl(str);
                }
                return true;
            } else {
                int key = getKeyCode(event.getKeyCode());
                if (key != 0) {
                    String str = "javascript:(function(){"
                            + "var customEvent = document.createEvent(\"HTMLEvents\");"
                            + "customEvent.initEvent(\"keyup\", true, true);"
                            + "customEvent.eventName = \"keyup\";"
                            + "customEvent.keyCode = customEvent.which = "
                            + key + ";"
                            + "document.dispatchEvent(customEvent);" + "})();";
                    WLog.i("dispatchKeyEvent", str);
                    if (mWebView != null) {
                        mWebView.loadUrl(str);
                    }
                    return true;
                } else {
                    return super.dispatchKeyEvent(event);
                }
            }
        } else if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (mWebView.getVisibility() == View.GONE) {
                return true;
            } else {
                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
                    return true;
                int key = getKeyCode(event.getKeyCode());
                if (key != 0) {
                    String str = "javascript:(function(){"
                            + "var customEvent = document.createEvent(\"HTMLEvents\");"
                            + "customEvent.initEvent(\"keydown\", true, true);"
                            + "customEvent.eventName = \"keydown\";"
                            + "customEvent.keyCode = customEvent.which = "
                            + key + ";"
                            + "document.dispatchEvent(customEvent);" + "})();";
                    WLog.i("dispatchKeyEvent", str);
                    mWebView.loadUrl(str);
                    return true;
                } else {
                    return super.dispatchKeyEvent(event);
                }
            }
        }

        return super.dispatchKeyEvent(event);
    }

    private int getKeyCode(int keycode) {
        int key = 0;
        switch (keycode) {
            case 19:
                key = 38;
                break;
            case 20:
                key = 40;
                break;
            case 21:
                key = 37;
                break;
            case 22:
                key = 39;
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
                key = 13;
                break;
            case KeyEvent.KEYCODE_0:
                key = 48;
                break;
            case KeyEvent.KEYCODE_1:
                key = 49;
                break;
            case KeyEvent.KEYCODE_2:
                key = 50;
                break;
            case KeyEvent.KEYCODE_3:
                key = 51;
                break;
            case KeyEvent.KEYCODE_4:
                key = 52;
                break;
            case KeyEvent.KEYCODE_5:
                key = 53;
                break;
            case KeyEvent.KEYCODE_6:
                key = 54;
                break;
            case KeyEvent.KEYCODE_7:
                key = 55;
                break;
            case KeyEvent.KEYCODE_8:
                key = 56;
                break;
            case KeyEvent.KEYCODE_9:
                key = 57;
                break;
            default:
                break;
        }
        return key;
    }
}
