package ai.pai.lensman.activities;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.common.executors.ThreadExecutor;
import com.android.common.utils.LogHelper;
import com.android.common.utils.NetworkUtil;

import org.json.JSONObject;

import java.util.HashMap;

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.utils.HttpPostTask;
import ai.pai.lensman.utils.SystemUtils;
import ai.pai.lensman.utils.UrlContainer;

public class PriceSettingActivity extends BaseActivity implements View.OnClickListener{

    private Button   sendBtn;
    private EditText originPriceET;
    private EditText noPrintPriceET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price_setting);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        int nomark = getIntent().getIntExtra("nomark",0);
        int origin = getIntent().getIntExtra("origin",0);
        TextView title = (TextView)findViewById(R.id.title_bar_middle_txt);
        title.setText(R.string.price_manage);
        originPriceET = (EditText)findViewById(R.id.et_price_origin);
        originPriceET.setText(String.valueOf(origin));
        originPriceET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sendBtn.setEnabled(s.toString().length()>0);
            }
        });

        noPrintPriceET = (EditText)findViewById(R.id.et_price_no_waterprint);
        noPrintPriceET.setText(String.valueOf(nomark));
        noPrintPriceET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sendBtn.setEnabled(s.toString().length()>0);
            }
        });

        sendBtn = (Button) findViewById(R.id.btn_price_setting);
        sendBtn.setOnClickListener(this);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_bar_back_layout:
                finish();
                break;
            case R.id.btn_price_setting:
                sendNewPrice();
                break;
            default:
                break;
        }
    }

    private void sendNewPrice() {
        if(!sendBtn.isEnabled()){
            return;
        }
        if(!NetworkUtil.isNetworkConnected(this)){
            Toast.makeText(this,R.string.network_disconnect,Toast.LENGTH_SHORT).show();
            return;
        }
        String originPrice = originPriceET.getText().toString();
        String noMarkPrice = noPrintPriceET.getText().toString();
        if(TextUtils.isEmpty(originPrice) && TextUtils.isEmpty(noMarkPrice)){
            return;
        }
        HashMap<String,String> params = new HashMap<String,String>();
        String userId = Preferences.getInstance().getLensManId();
        if(userId.length()==0){
            return;
        }
        sendBtn.setEnabled(false);
        params.put("user_id",userId);
        params.put("nomark",noMarkPrice);
        params.put("origin",originPrice);
        new HttpPostTask(params){
            @Override
            protected boolean parseResponse(String response) {
                LogHelper.d(TAG,"PriceSettingActivity get response string = "+response);
                try{
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if(status == 200){
                        return true;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                sendBtn.setEnabled(true);
                Toast.makeText(PriceSettingActivity.this,R.string.set_price_success,Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
                sendBtn.setEnabled(true);
                Toast.makeText(PriceSettingActivity.this,R.string.set_price_fail,Toast.LENGTH_SHORT).show();
            }
        }.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.PRICE_FIX_URL);
    }
}
