package kr.co.icontrols.iotplatform;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

import kr.co.icontrols.iotplatform.DataClass.UserInfo;

/**
 * 메인 메뉴 컴퍼넌트.
 */
public class MainMenu extends LinearLayout {

    private String TAG = MainMenu.class.getSimpleName();
    private static Class<?> mOldActivity = null;

    public static final int MAIN_MENU_HOME = 1;
    public static final int MAIN_MENU_DEVICE = 2;
    public static final int MAIN_MENU_MY_GROUP = 3;
    public static final int MAIN_MENU_PROFILE = 4;

    private int mSelectedIndex = 0;
    private Context mContext;
    private static boolean mIsStartNextActivity = false;

    public MainMenu(Context context) {
        this(context,null);
        mContext = context;
    }

    public MainMenu(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mContext = context;

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MainMenu, 0, 0);
        mSelectedIndex = a.getInteger(R.styleable.MainMenu_selectedIndex,0);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main_menu, this, true);

        initComponents(mSelectedIndex);
    }

    private void initComponents(int index) {
        switch (index) {
            case MAIN_MENU_HOME:
                findViewById(R.id.layoutMenu01).setSelected(true);
                findViewById(R.id.layoutMenu02).setSelected(false);
                findViewById(R.id.layoutMenu03).setSelected(false);
                findViewById(R.id.layoutMenu04).setSelected(false);
                findViewById(R.id.txtMenu01).setSelected(true);
                findViewById(R.id.txtMenu02).setSelected(false);
                findViewById(R.id.txtMenu03).setSelected(false);
                findViewById(R.id.txtMenu04).setSelected(false);
                break;
            case MAIN_MENU_DEVICE:
                findViewById(R.id.layoutMenu01).setSelected(false);
                findViewById(R.id.layoutMenu02).setSelected(true);
                findViewById(R.id.layoutMenu03).setSelected(false);
                findViewById(R.id.layoutMenu04).setSelected(false);
                findViewById(R.id.txtMenu01).setSelected(false);
                findViewById(R.id.txtMenu02).setSelected(true);
                findViewById(R.id.txtMenu03).setSelected(false);
                findViewById(R.id.txtMenu04).setSelected(false);
                break;
            case MAIN_MENU_MY_GROUP:
                findViewById(R.id.layoutMenu01).setSelected(false);
                findViewById(R.id.layoutMenu02).setSelected(false);
                findViewById(R.id.layoutMenu03).setSelected(true);
                findViewById(R.id.layoutMenu04).setSelected(false);
                findViewById(R.id.txtMenu01).setSelected(false);
                findViewById(R.id.txtMenu02).setSelected(false);
                findViewById(R.id.txtMenu03).setSelected(true);
                findViewById(R.id.txtMenu04).setSelected(false);
                break;
            case MAIN_MENU_PROFILE:
                findViewById(R.id.layoutMenu01).setSelected(false);
                findViewById(R.id.layoutMenu02).setSelected(false);
                findViewById(R.id.layoutMenu03).setSelected(false);
                findViewById(R.id.layoutMenu04).setSelected(true);
                findViewById(R.id.txtMenu01).setSelected(false);
                findViewById(R.id.txtMenu02).setSelected(false);
                findViewById(R.id.txtMenu03).setSelected(false);
                findViewById(R.id.txtMenu04).setSelected(true);
                break;
            default:
                findViewById(R.id.layoutMenu01).setSelected(false);
                findViewById(R.id.layoutMenu02).setSelected(false);
                findViewById(R.id.layoutMenu03).setSelected(false);
                findViewById(R.id.layoutMenu04).setSelected(false);
                findViewById(R.id.txtMenu01).setSelected(false);
                findViewById(R.id.txtMenu02).setSelected(false);
                findViewById(R.id.txtMenu03).setSelected(false);
                findViewById(R.id.txtMenu04).setSelected(false);
                break;
        }

        findViewById(R.id.layoutMenu01).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectedIndex != MAIN_MENU_HOME) {
                    changeActvityForMainMenu(MAIN_MENU_HOME, getContext());
                }
            }
        });
        findViewById(R.id.layoutMenu02).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectedIndex != MAIN_MENU_DEVICE) {
                    changeActvityForMainMenu(MAIN_MENU_DEVICE, getContext());
                }
            }
        });
        findViewById(R.id.layoutMenu03).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectedIndex != MAIN_MENU_MY_GROUP) {
                    changeActvityForMainMenu(MAIN_MENU_MY_GROUP, getContext());
                }
            }
        });
        findViewById(R.id.layoutMenu04).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectedIndex != MAIN_MENU_PROFILE) {
                    changeActvityForMainMenu(MAIN_MENU_PROFILE, getContext());
                }
            }
        });
    }

    private void changeActvityForMainMenu(int index, final Context context) {
        if(!UserInfo.confirmed) {
            return;
        }

        mIsStartNextActivity = true;
        mOldActivity = ((Activity) context).getClass();
        Intent intent;
        switch (index) {
            case MAIN_MENU_HOME:
                intent = new Intent(context, MainActivity.class);
                break;
            case MAIN_MENU_DEVICE:
                intent = new Intent(context, DeviceActivity.class);
                break;
            case MAIN_MENU_MY_GROUP:
                intent = new Intent(context, MyGroupActivity.class);
                break;
            case MAIN_MENU_PROFILE:
                intent = new Intent(context, ProfileActivity.class);
                break;
            default:
                intent = new Intent(context, MainActivity.class);
                break;
        }
        context.startActivity(intent);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        ((Activity) context).overridePendingTransition(0, 0);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                ((Activity) context).finish();
                mIsStartNextActivity = false;
            }
        }, 500);

    }
}
