package kr.co.hanwha.hifive.view;

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

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

public class HFSettingProfileView extends LinearLayout {
    private TextView mTextViewName;
    private TextView mTextViewDepartmentPosition;
    private View mViewDepartmentLine;
    private TextView mTextViewId;

    private LinearLayout mLayoutDepartment;
    private TextView mTextViewPosition2;
    private View mViewLine2;
    private TextView mTextViewDepartment;
    private ImageView mImageViewProfile;

    private OnSettingProfileListener onSettingProfileListener;

    public void setOnSettingProfileListener(OnSettingProfileListener onSettingProfileListener) {
        this.onSettingProfileListener = onSettingProfileListener;
    }

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

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

    public HFSettingProfileView(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_profile, this, true);

        initLayout();

        initCallback();
    }

    private void initLayout() {
        mTextViewName = (TextView) findViewById(R.id.name_textView);
        mTextViewDepartmentPosition = (TextView) findViewById(R.id.departmentPosition_textView);
        mViewDepartmentLine = (View) findViewById(R.id.departmentLine_view);
        mTextViewId = (TextView) findViewById(R.id.id_textView);

        mLayoutDepartment = (LinearLayout) findViewById(R.id.department_linearLayout);
        mTextViewPosition2 = (TextView) findViewById(R.id.position2_textView);
        mViewLine2 = (View) findViewById(R.id.line2_view);
        mTextViewDepartment = (TextView) findViewById(R.id.department_textView);
        mImageViewProfile = (ImageView) findViewById(R.id.profile_imageView);

        GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewProfile);
    }

    private void initCallback() {
        mImageViewProfile.setOnClickListener(onClickListener);
    }

    public void setMemberInfo(@NonNull LoginData loginData) {
        if (loginData == null) return;

        mTextViewName.setText(loginData.getName());

        mTextViewId.setText(loginData.getLogin());

        String position = loginData.getPosition();
        String department = HFInfoUtil.getDepartmentText(getContext());
        setDepartmentView(department, position);
    }

    private void setDepartmentView(@Nullable String department, @Nullable String position) {
        if (TextUtils.isEmpty(department)) {
            if (TextUtils.isEmpty(position)) {
                mViewLine2.setVisibility(View.GONE);
                mTextViewPosition2.setVisibility(View.GONE);
            } else {
                mTextViewPosition2.setText(position);
                mViewLine2.setVisibility(View.VISIBLE);
                mTextViewPosition2.setVisibility(View.VISIBLE);
            }

            mLayoutDepartment.setVisibility(View.GONE);
        } else {
            mViewLine2.setVisibility(View.GONE);
            mTextViewPosition2.setVisibility(View.GONE);

            if (TextUtils.isEmpty(position)) {
                mViewDepartmentLine.setVisibility(View.GONE);
                mTextViewDepartmentPosition.setVisibility(View.GONE);
            } else {
                mTextViewDepartmentPosition.setText(position);
                mViewDepartmentLine.setVisibility(View.VISIBLE);
                mTextViewDepartmentPosition.setVisibility(View.VISIBLE);
            }

            mLayoutDepartment.setVisibility(View.VISIBLE);
            mTextViewDepartment.setText(department);
        }
    }

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

    /**************************************************
     * View.OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.profile_imageView: {
                    testIndex += 1;
                    testHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            testIndex = 0;

                        }
                    }, 5000);
                    if (testIndex > 5 && testIndex < 10) {
                        if (onSettingProfileListener != null)
                            onSettingProfileListener.onEasterEggClicked();
                    }
                    break;
                }
            }
        }
    };

    /**************************************************
     * OnSettingProfileListener - interface
     **************************************************/
    public interface OnSettingProfileListener {
        void onEasterEggClicked();
    }
}