/*
 * Copyright (c) 2017. nTels.co.,ltd. to Present.
 * All rights reserved.
 */

package com.ntels.onecable.view.rules;

import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorSelectedListener;
import com.ntels.onecable.R;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

/**
 * <pre>
 * 1. ClassName :   RulesColorSelect
 * 2. FileName  :   RulesColorSelect.java
 * 3. Package   :   com.ntels.onecable.view.rules
 * 4. Date      :   2017-01-02
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class RulesColorSelect extends CustomActivity implements View.OnClickListener {

    private Util util = new Util();
    private Intent intent;
    private Activity act;

    private String mSensorName;
    private int mSelectedColor = -1;
    private ColorPickerView mColorPickerView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = RulesColorSelect.this;

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.rules_color_select, null);
        this.setContentView(viewToLoad);

        if (null != getIntent()) {
            Intent intent = getIntent();
            mSensorName = util.null2string(intent.getStringExtra(Constants.EXTRA_NAME));
        }

        displayView();
    }

    /**
     * UI display.
     */
    private void displayView() {

        util.getTextView(act, R.id.tvTitle).setText(mSensorName);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
        mColorPickerView = (ColorPickerView) findViewById(R.id.color_picker_view);
        mColorPickerView.addOnColorSelectedListener(new OnColorSelectedListener() {
            @Override
            public void onColorSelected(int selectedColor) {
                mSelectedColor = selectedColor;
            }
        });
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                if (mSelectedColor >= -1) {
                    util.setShowToast(act, util.getString(act, R.string.MSG922),
                            Toast.LENGTH_SHORT);
                    return;
                }
                intent = new Intent();
                intent.putExtra(Constants.EXTRA_SELECTED_COLOR,
                        util.getHsbStr(util.rgbToHsb(mSelectedColor)));
                act.setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
    }
}
