package ca.antonious.viewcelladapter.viewcells.builtins;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import ca.antonious.viewcelladapter.R;
import ca.antonious.viewcelladapter.annotations.BindListener;
import ca.antonious.viewcelladapter.internal.Function;
import ca.antonious.viewcelladapter.viewcells.AbstractViewCell;
import ca.antonious.viewcelladapter.viewcells.BaseViewHolder;

/**
 * Created by George on 2017-04-26.
 */

public class MaterialToggleSettingViewCell extends AbstractViewCell<MaterialToggleSettingViewCell.MaterialToggleSettingViewHolder> {
    private int settingId;
    private String settingName;
    private boolean isChecked;

    public MaterialToggleSettingViewCell(int settingId,
                                         String settingName,
                                         boolean isChecked)
    {
        this.settingId = settingId;
        this.settingName = settingName;
        this.isChecked = isChecked;
    }

    @Override
    public int getLayoutId() {
        return R.layout.material_toggle_setting_list_item;
    }

    @Override
    public int getItemId() {
        return settingId;
    }

    @Override
    public Function<View, BaseViewHolder> getViewHolderFactory() {
        return new Function<View, BaseViewHolder>() {
            @Override
            public BaseViewHolder apply(View view) {
                return new MaterialToggleSettingViewHolder(view);
            }
        };
    }

    @Override
    public void bindViewCell(MaterialToggleSettingViewHolder viewHolder) {
        viewHolder.setSettingName(settingName);
        viewHolder.setSwitchState(isChecked);
    }

    public interface OnSettingToggledListener {
        void onSettingToggled(int settingId, boolean isOn);
    }

    @BindListener(bindIfNull = true)
    public void bindOnSettingToggledListener(final MaterialToggleSettingViewHolder viewHolder, final OnSettingToggledListener listener) {
        viewHolder.setOnBackgroundClickedListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.setSwitchState(!isChecked);
            }
        });

        viewHolder.setOnSwitchStateChangedListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                isChecked = checked;
                if (listener != null) {
                    listener.onSettingToggled(settingId, checked);
                }
            }
        });
    }

    public class MaterialToggleSettingViewHolder extends BaseViewHolder {
        private TextView settingNameTextView;
        private Switch settingSwitch;

        public MaterialToggleSettingViewHolder(View itemView) {
            super(itemView);
            settingNameTextView = (TextView) itemView.findViewById(R.id.setting_name);
            settingSwitch = (Switch) itemView.findViewById(R.id.setting_switch);
        }

        public void setSettingName(String settingName) {
            settingNameTextView.setText(settingName);
        }

        public void setSwitchState(boolean isChecked) {
            settingSwitch.setChecked(isChecked);
        }

        public void setOnBackgroundClickedListener(View.OnClickListener onClickListener) {
            itemView.setOnClickListener(onClickListener);
        }

        public void setOnSwitchStateChangedListener(CompoundButton.OnCheckedChangeListener onSwitchStateChangedListener) {
            settingSwitch.setOnCheckedChangeListener(onSwitchStateChangedListener);
        }
    }
}

@@ -906,20 +906,16 @@
 Checked)
-%0A   
  %7B%0A     
@@ -1020,32 +1020,106 @@
 Checked;%0A    %7D%0A%0A
+    public static Builder create() %7B%0A        return new Builder();%0A    %7D%0A%0A
     @Override%0A  
@@ -3762,11 +3762,800 @@
  %7D%0A    %7D
+%0A%0A    public static class Builder %7B%0A        private int settingId;%0A        private String settingName;%0A        private boolean isChecked;%0A%0A        public Builder() %7B%0A            this.settingId = -1;%0A            this.settingName = %22%22;%0A            this.isChecked = false;%0A        %7D%0A%0A        public Builder id(int id) %7B%0A            this.settingId = id;%0A            return this;%0A        %7D%0A%0A        public Builder name(String name) %7B%0A            this.settingName = name;%0A            return this;%0A        %7D%0A%0A        public Builder checked(boolean isChecked) %7B%0A            this.isChecked = isChecked;%0A            return this;%0A        %7D%0A%0A        public MaterialToggleSettingViewCell build() %7B%0A            return new MaterialToggleSettingViewCell(settingId, settingName, isChecked);%0A        %7D%0A    %7D
 %0A%7D%0A

Add builder for the settings toggle