package com.tcl.customerapi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemProperties;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.net.URI;

public class MyReceiver extends BroadcastReceiver {
    //	private static final String MSG_ = "com.tcl.customerapi.MyReceiver";

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        String tmp = arg1.getAction();

        Log.i("api", "onReceive   " + tmp);
        if (tmp.equalsIgnoreCase(defs.MSG_CHANNEL_BACKUP)) {

            Toast.makeText(arg0, "频道导出成功！", Toast.LENGTH_LONG).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_CHANNEL_RESTORE)) {

            Toast.makeText(arg0, "频道导入成功！", Toast.LENGTH_LONG).show();

        } else if (tmp.equalsIgnoreCase(defs.MSG_CHANNEL_ERR)) {

            Toast.makeText(arg0, "设置无效！", Toast.LENGTH_LONG).show();

        } else if (tmp.equalsIgnoreCase(defs.MSG_SETTING_ERR)) {


            Toast.makeText(arg0, "设置失败！", Toast.LENGTH_LONG).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_SETTING_OK)) {

            Toast.makeText(arg0, "设置成功！", Toast.LENGTH_LONG).show();


        } else if (tmp.equalsIgnoreCase(defs.MSG_NO_USB_MOUNT)) {
            Toast.makeText(arg0, "NO_USB_MOUNT", Toast.LENGTH_SHORT).show();

        } else if (tmp.equalsIgnoreCase(defs.MSG_USB_TO_NVM_OK)) {

            Toast.makeText(arg0, "SET_OK", Toast.LENGTH_SHORT).show();

        } else if (tmp.equalsIgnoreCase(defs.MSG_USB_TO_NVM_NG)) {

            Toast.makeText(arg0, "SET_NG", Toast.LENGTH_SHORT).show();


        } else if (tmp.equalsIgnoreCase(defs.MSG_NVM_TO_USB_NG)) {

            Toast.makeText(arg0, "SET_NG", Toast.LENGTH_SHORT).show();

        } else if (tmp.equalsIgnoreCase(defs.MSG_NVM_TO_USB_OK)) {

            Toast.makeText(arg0, "SET_OK", Toast.LENGTH_SHORT).show();


        } else if (tmp.equalsIgnoreCase(defs.MSG_LOGO_RESOLUTION_ERR)) {
            //"Error!! The resolution of the logo isn't 1920x1080 or 1280x720!!"
//			Toast.makeText(arg0, "Error!!Copy logo.jpg fail!!", Toast.LENGTH_SHORT).show();
            Toast.makeText(arg0, "Error!! The resolution of the logo isn't 1920x1080 or 1280x720!!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_LOGO_SIZE_ERR)) {

            Toast.makeText(arg0, "Error!! The size of the logo file is larger then 1M !", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_USB_NO_BOOTANIMATION)) {

            Toast.makeText(arg0, "Error!! Not found bootanimation.zip in U disk !!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_USB_NO_BOOTVIDEO)) {

            Toast.makeText(arg0, "Error!! Not found bootvideo.ts or ts.ini in U disk !!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_COPY_BOOTVIDEO_FAIL)) {

            Toast.makeText(arg0, "Error!! Copy bootvideo.ts or ts.ini file fail!!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_SET_UP_SUCCESS)) {
            String isShowLogoTotast = SystemProperties.get(defs.SHOW_LOGO_TOAST, "true");
            if (isShowLogoTotast.equals("true")) {
                Toast.makeText(arg0, "Set up success!!", Toast.LENGTH_SHORT).show();
            } else {
                SystemProperties.set(defs.SHOW_LOGO_TOAST, "true");
            }
        } else if (tmp.equalsIgnoreCase(defs.MSG_SET_UP_FAIL)) {

            Toast.makeText(arg0, "Error!!Set up fail!!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_BOOTANIMATION_LARGER)) {

            Toast.makeText(arg0, "Error!! The size of the file is larger then 30M !", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_NO_SPACE_TO_SAVE)) {

            Toast.makeText(arg0, "Error!!No space to save file!!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_USB_BOOTANIMATION_COPY_FAIL)) {

            Toast.makeText(arg0, "Error!!Copy animation.zip file fail!!", Toast.LENGTH_SHORT).show();
        } else if (tmp.equalsIgnoreCase(defs.MSG_LOGO_COPY_ERR)) {

            Toast.makeText(arg0, "Error!!Copy logo.jpg fail!!", Toast.LENGTH_SHORT).show();

        } else if (tmp.equalsIgnoreCase(defs.BOOT_COMPLETED) || tmp.equalsIgnoreCase(defs.BOOT_P0)) {
            Log.i("api", "power on and check tlc_oatfile  !");
            if (FileUtil.isFile(defs.otaFilePath)) {
                Log.i("api", "power on and is tcl_oatfile !");
                String ACTION = "action.tvcustomer.api";
                Intent intent = new Intent(ACTION);
                intent.setPackage("com.tcl.customerapi");
                arg0.startService(intent);
            }
            startAppUpdateService(arg0);
        } else if (defs.MSG_TEST.equals(tmp)) {
            Log.i("api", "receive action:" + defs.MSG_TEST);
        } else if(tmp.equals(Intent.ACTION_MEDIA_MOUNTED)){
            String pathUri = arg1.getDataString();
            File file = new File(URI.create(pathUri));
            String path = file.getAbsolutePath();
            Log.d("api", "Media path: " + path + ", pathUri:" + pathUri);
            TclApplication.UdiskPATH = path;
        }
    }

    private void startAppUpdateService(Context context) {
        Log.i("api", "start app update service");
        String ACTION = "action.tvcustomer.appupdate";
        Intent intent = new Intent(ACTION);
        intent.setPackage("com.tcl.customerapi");
        context.startService(intent);
    }
}
