package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

public class HFSettingMenuVersionView extends LinearLayout {
    private View mViewTopLine;
    private TextView mTextViewVersion;
    private TextView mTextViewUpdate;

    private OnSettingMenuVersionListener onSettingMenuVersionListener;

    public void setOnSettingMenuVersionListener(OnSettingMenuVersionListener onSettingMenuVersionListener) {
        this.onSettingMenuVersionListener = onSettingMenuVersionListener;
    }

    public HFSettingMenuVersionView(Context context) {
        super(context);
        initialize(context, null);
    }

    public HFSettingMenuVersionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public HFSettingMenuVersionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
//        if (isInEditMode()) return;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_setting_menu_version, this, true);

        initLayout();

        initAttributeSet(context, attrs);

        initCallback();
    }

    private void initLayout() {
        mViewTopLine = (View) findViewById(R.id.topLine_view);
        mTextViewVersion = (TextView) findViewById(R.id.version_textView);
        mTextViewUpdate = (TextView) findViewById(R.id.update_textView);
    }

    private void initAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
        if (attrs != null) {
            TypedArray type = null;
            try {
                type = context.obtainStyledAttributes(attrs, R.styleable.SettingView);
                boolean isTopLine = type.getBoolean(R.styleable.SettingView_topLine, false);

                mViewTopLine.setVisibility(isTopLine ? View.VISIBLE : View.GONE);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void initCallback() {
        mTextViewUpdate.setOnClickListener(onClickListener);
        mTextViewVersion.setOnClickListener(onClickListener);
    }


    public void setVersion(@NonNull String version, @NonNull boolean needUpdate) {
        mTextViewVersion.setText(version);

        mTextViewUpdate.setVisibility(needUpdate ? View.VISIBLE : View.GONE);
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (onSettingMenuVersionListener != null) {
                switch (view.getId()) {
                    case R.id.version_textView:

                        testIndex += 1;
                        testHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                testIndex = 0;

                            }
                        }, 5000);
                        if (testIndex > 10 && testIndex < 20) {
                            onSettingMenuVersionListener.onEasterEggClicked();
                        }
                        break;
                    case R.id.update_textView:
                        onSettingMenuVersionListener.onUpdateClicked();
                        break;
                }
            }
        }
    };

    Handler testHandler = new Handler();
    int testIndex = 0;

    /**************************************************
     * OnSettingMenuVersionListener - interface
     **************************************************/
    public interface OnSettingMenuVersionListener {
        void onUpdateClicked();

        void onEasterEggClicked();
    }
}