package com.tcl.factory.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.ActivityManager;

import java.util.ArrayList;

import android.content.pm.IPackageDataObserver;
import android.os.SystemProperties;

import com.tcl.factory.FactoryManager;
import com.tcl.factory.bsp.TCL;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.ir.IRManager;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;
import com.tcl.tosapi.utility.PropertyApi;

public class BootUpReceive extends BroadcastReceiver {
    public final static int D_MODE = 1;
    public final static int P_MODE = 2;
    public final static int W_MODE = 3;
    public final static int S_MODE = 4;

    public final int OSD_SHOW = 1;
    public final int OSD_HIDE = 0;
    private static int ReceiveEvent = 0;
    private ClearUserDataObserver mClearDataObserver;
    private Context mContext;
    private final static String TAG = "BootUpReceive";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mContext = context;
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals("android.intent.action.BOOT_P1")) {
            //Debug.v("BootUpReceive action = ",intent.getAction().toString());
            if (ReceiveEvent == 0) {
                ReceiveEvent = 1;
                Debug.v(TAG, "BootUpReceive in");

                TCL.implemetThirdPartyIsntance(context);

                boolean valueW = ThirdParty.getWModeStatus();//mFactoryAdapter.readValueFromTVStorage(0, ConfigType.ADDR_WMODE_FLAG);
                boolean valueD = ThirdParty.getDModeStatus();//.readValueFromTVStorage(0, ConfigType.ADDR_DMODE_FLAG);
                boolean valueP = ThirdParty.getPModeStatus();//.readValueFromTVStorage(0, ConfigType.ADDR_PMODE_FLAG);

                if (valueD) {
                    Debug.v(TAG, "in (valueD==1)");
                    if (valueW) {
                        Debug.v(TAG, "valueW==1");
                        Intent it = new Intent();
                        it.putExtra("select", W_MODE);
                        it.putExtra("osdlabel", OSD_SHOW);
                        it.setClassName("com.tcl.factory.view", "com.tcl.factory.view.MainMenu");
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Jane add
                        context.startActivity(it);
                    } else if (valueP) {
                        Debug.v(TAG, "valueP==1");
                        Intent it = new Intent();
                        it.setClass(context, InfoService.class);//longch 0802
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Jane add
                        it.putExtra("mode", 0);
                        context.startService(it);
                    } else {
                        Debug.v(TAG, "(valueD==1)");
                        Intent it = new Intent();
                        it.setClass(context, InfoService.class);//longch 0802
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Jane add
                        it.putExtra("mode", 0);
                        context.startService(it);
                    }
                } else if ((valueP)) {
                    //				if(valueP == 0xff) {
                    //					Debug.v("BootUpReceive", "in valueP=="+valueP);
                    //					mFactoryAdapter.writeValue2TVStorage(0, ConfigType.ADDR_PMODE_FLAG, 1);
                    //				}
                    Debug.v(TAG, "in valueP==1");
                    String lanNumStr = PropertyApi.getInstance().get("factory.skd.set.language", "-1");
                    int lanNum = Integer.valueOf(lanNumStr);
                    //Log.i("BootUpReceive","language number is " + lanNum);
                    if (lanNum >= 0) {
                        IRManager.parserSerial(0x72, lanNum);
                        PropertyApi.getInstance().set("factory.skd.set.language", "-1");
                    }

                    if (valueW) {
                        Debug.v(TAG, "valueW==1");
                        Intent it = new Intent();
                        it.putExtra("select", W_MODE);
                        it.putExtra("osdlabel", OSD_SHOW);
                        it.setClassName("com.tcl.factory.view", "com.tcl.factory.view.MainMenu");
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Jane add
                        context.startActivity(it);
                    } else {
                        Debug.v(TAG, "valueP==1");
                        Intent it = new Intent();
                        it.setClass(context, InfoService.class);//longch 0802
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Jane add
                        it.putExtra("mode", 0);
                        context.startService(it);
                    }
                } else {        //for hotel
                    int hotelmode = ThirdParty.getHotelFlag();
                    Debug.v(TAG, "in getHotelFlag==" + hotelmode);
                    String hotelEnable = SystemProperties.get(DEFS.HOTEL_PROPERTY_HOTEL_HOTKEY, "0");
                    if (hotelmode == 1) {
                        if (!hotelEnable.equals("1")) {
                            SystemProperties.set(DEFS.HOTEL_PROPERTY_HOTEL_HOTKEY, "1");
                        }
                        hotel_cleanBrowerData();
                    } else {
                        if (hotelEnable.equals("1")) {
                            SystemProperties.set(DEFS.HOTEL_PROPERTY_HOTEL_HOTKEY, "0");
                            String cmd = "rm -f /data/property/" + DEFS.HOTEL_PROPERTY_HOTEL_HOTKEY;
                            ThirdParty.setRootSeek(cmd);
                        }
                    }
                }
            }
        }
    }

    private void hotel_cleanBrowerData() {
        mClearDataObserver = new ClearUserDataObserver();
        ActivityManager am = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        boolean res = false;
        try {
//			res = am.clearApplicationUserData("com.tcl.browser",mClearDataObserver);
//			Debug.v( TAG,"package name=com.tcl.browser"+ ",result = " + res);
//			res = am.clearApplicationUserData("com.google.android.tv.frameworkpackagestubs",mClearDataObserver);
            Debug.v(TAG, "package name=com.google.android.tv.frameworkpackagestubs" + ",result = " + res);
        } catch (Exception e) {
            Debug.v(TAG, e.toString());
        }
    }

    class ClearUserDataObserver extends IPackageDataObserver.Stub {
        public void onRemoveCompleted(final String packageName,
                                      final boolean succeeded) {
            Debug.v(TAG, "UserData =" + packageName + ",result = " + succeeded);
        }
    }
}
