package io.github.droidkaigi.confsched2017.view.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import io.github.droidkaigi.confsched2017.R;
import io.github.droidkaigi.confsched2017.databinding.ViewSettingSwitchRowBinding;

public class SettingSwitchRowView extends RelativeLayout {

    private static final String TAG = SettingSwitchRowView.class.getSimpleName();

    private ViewSettingSwitchRowBinding binding;

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    public SettingSwitchRowView(Context context) {
        this(context, null);
    }

    public SettingSwitchRowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingSwitchRowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (!isInEditMode()) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_setting_switch_row, this, true);
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingSwitchRow);

            String title = a.getString(R.styleable.SettingSwitchRow_settingTitle);
            String description = a.getString(R.styleable.SettingSwitchRow_settingDescription);

            binding.settingTitle.setText(title);
            binding.settingDescription.setText(description);

            binding.getRoot().setOnClickListener(v -> toggle());
            binding.settingSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (onCheckedChangeListener != null) {
                    onCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
                }
            });

            a.recycle();
        } else {
            LayoutInflater.from(context).inflate(R.layout.view_setting_switch_row, this, true);
        }
    }

    private void toggle() {
        boolean isChecked = binding.settingSwitch.isChecked();
        binding.settingSwitch.setChecked(!isChecked);
    }

    public void init(boolean defaultValue, CompoundButton.OnCheckedChangeListener listener) {
        setDefault(defaultValue);
        setOnCheckedChangeListener(listener);
    }

    public void setDefault(boolean defaultValue) {
        binding.settingSwitch.setChecked(defaultValue);
    }

    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
        this.onCheckedChangeListener = listener;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        binding.getRoot().setEnabled(enabled);
        binding.settingSwitch.setEnabled(enabled);
        if (enabled) {
            binding.settingTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
            binding.settingDescription.setTextColor(ContextCompat.getColor(getContext(), R.color.grey600));
        } else {
            int disabledTextColor = ContextCompat.getColor(getContext(), R.color.black_alpha_30);
            binding.settingTitle.setTextColor(disabledTextColor);
            binding.settingDescription.setTextColor(disabledTextColor);
        }
    }
}

@@ -1096,17 +1096,16 @@
 eAttr);%0A
-%0A
         
@@ -1108,17 +1108,16 @@
     if (
-!
 isInEdit
@@ -1122,32 +1122,129 @@
 itMode()) %7B%0A    
+        inflate(context, R.layout.view_setting_switch_row, this);%0A            return;%0A        %7D%0A%0A
         binding 
@@ -1354,20 +1354,16 @@
         
-   
- 
 TypedArr
@@ -1443,28 +1443,24 @@
 );%0A%0A        
-    
 String title
@@ -1514,28 +1514,24 @@
 tingTitle);%0A
-    
         Stri
@@ -1610,36 +1610,32 @@
 tion);%0A%0A        
-    
 binding.settingT
@@ -1651,28 +1651,24 @@
 ext(title);%0A
-    
         bind
@@ -1713,36 +1713,32 @@
 tion);%0A%0A        
-    
 binding.getRoot(
@@ -1770,28 +1770,24 @@
  toggle());%0A
-    
         bind
@@ -1868,28 +1868,24 @@
             
-    
 if (onChecke
@@ -1907,28 +1907,24 @@
  != null) %7B%0A
-    
             
@@ -2004,26 +2004,18 @@
         
-    %7D%0A    
+%7D%0A
         
@@ -2027,20 +2027,16 @@
         
- 
-   
 a.recycl
@@ -2044,131 +2044,8 @@
 ();%0A
-        %7D else %7B%0A            LayoutInflater.from(context).inflate(R.layout.view_setting_switch_row, this, true);%0A        %7D%0A
     

Simplify inflate style in edit mode