package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.content.res.TypedArray;
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.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;

public class HFSettingMenuActionView extends LinearLayout {
    private View mViewTopLine;
    private TextView mTextViewName;
    private TextView mTextViewSubName;
    private ImageView mImageViewArrow;
    private Switch mSwitchOnOff;

    private OnSettingMenuActionListener onSettingMenuActionListener;

    public void setOnSettingMenuActionListener(OnSettingMenuActionListener onSettingMenuActionListener) {
        this.onSettingMenuActionListener = onSettingMenuActionListener;
    }

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

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

    public HFSettingMenuActionView(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_action, this, true);

        initLayout();

        initAttributeSet(context, attrs);

        initCallback();
    }

    private void initLayout() {
        mViewTopLine = (View) findViewById(R.id.topLine_view);
        mTextViewName = (TextView) findViewById(R.id.name_textView);
        mTextViewSubName = (TextView) findViewById(R.id.subName_textView);
        mImageViewArrow = (ImageView) findViewById(R.id.arrow_imageView);
        mSwitchOnOff = (Switch) findViewById(R.id.onoff_switch);
    }

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

                if (TextUtils.isEmpty(name) == false)
                    mTextViewName.setText(name);

                if (TextUtils.isEmpty(subName) == false)
                    mTextViewSubName.setText(subName);

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

                if (onOffSwitch) {
                    mImageViewArrow.setVisibility(View.GONE);
                    mSwitchOnOff.setVisibility(View.VISIBLE);
                } else {
                    mImageViewArrow.setVisibility(View.VISIBLE);
                    mSwitchOnOff.setVisibility(View.GONE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void initCallback() {
        mSwitchOnOff.setOnCheckedChangeListener(onCheckedChangeListener);
    }

    public void setSubName(String subName) {
        if (subName == null) return;
        mTextViewSubName.setText(subName);
    }

    public void setAlarmSetting(boolean isChecked) {
        if (mSwitchOnOff.isChecked() != isChecked)
            mSwitchOnOff.setChecked(isChecked);
    }

    /**************************************************
     * CompoundButton.OnCheckedChangeListener
     **************************************************/
    public CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (onSettingMenuActionListener != null)
                onSettingMenuActionListener.onCheckedChanged(isChecked);
        }
    };


    /**************************************************
     * OnSettingProfileListener - interface
     **************************************************/
    public interface OnSettingMenuActionListener {
        void onCheckedChanged(boolean isChecked);
    }
}