package kr.co.icontrols.ilp300_700.screen;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.RelativeLayout;

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.R;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.POSITION;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.ID;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;

public class Reboot extends WpadScreen {

    String TAG = "Reboot";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = true;   // true, false
    boolean bLOG_INFO = true;   // true, false

    WpadImageView Image_Background;

    Context mContext;

    CountDownTimer CntDnTimer;

    int nInterval = 1000;
    int nTotalInitStep = 2;
    int nInitTotalTime = nInterval * (nTotalInitStep + 1);
    int nInitStep = 0;

    int[] RebootBGImages = {R.drawable.reboot_back, R.drawable.reboot_back_700};

    public Reboot(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - Reboot] -------------------");

            mContext = context;
            Initialize();

            Image_Background = new WpadImageView(context, 0, Declare.DeviceInfo.getScreenSizeFullW(ConfigValue.DEVICE_MODELTYPE), Declare.DeviceInfo.getScreenSizeFullH(ConfigValue.DEVICE_MODELTYPE), RebootBGImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.REBOOT.ELEMENTS.IMAGES.BACKGROUND);
            ViewRegistration(layout, Image_Background, getPosition(ID.SCREENS.REBOOT.ELEMENTS.IMAGES.BACKGROUND).X, getPosition(ID.SCREENS.REBOOT.ELEMENTS.IMAGES.BACKGROUND).Y);

        } catch (Exception e) {
            Log.e(TAG, "[Exception] Reboot(...) : " + e);
            e.printStackTrace();
        }
    }

    private void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    private void Initialize() {
        try {
            initCntDnTimer();
            CntDnTimer.start();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - Reboot] -------------------");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onClose(...) : " + e);
            e.printStackTrace();
        }
        super.onClose();
    }

    public POSITION getPosition(int nID) {
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.INIT.ELEMENTS.IMAGES.BACKGROUND:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(0, 0, 800, 480);
                    else position.SetPosition(0, 0, 480, 272);
                    return position;

                default:
                    return position;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getPosition() : " + e);
            e.printStackTrace();
            return position;
        }
    }

    private void initCntDnTimer() {
        try {
            LOG("[CntDnTimer] = START =");

            CntDnTimer = new CountDownTimer(nInitTotalTime, nInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    nInitStep++;
                    LOG("[CntDnTimer] = onTick = [" + nInitStep + "]");
                }

                @Override
                public void onFinish() {
                    LOG("[CntDnTimer] ===== onFinish =====");
                    LOG("[CntDnTimer] = onFinish = [" + nInitStep + "]");
                    ((MainActivity) mContext).startSystemReboot();
                }
            };
        } catch (Exception e) {
            Log.e(TAG, "[Exception] CntDnTimer() : " + e);
            e.printStackTrace();
        }
    }

}