package tw.com.altob.goodpk.Fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.loader.content.Loader;

import com.ga.sdk.api.ApiResult;

import java.io.InputStream;

import tw.com.altob.goodpk.Bean.CW;
import tw.com.altob.goodpk.Loader.ApiLoaderCallback;
import tw.com.altob.goodpk.Loader.GetCarWhereLoader;
import tw.com.altob.goodpk.R;

public class InquireCarFragment extends Fragment {

    private Button OKBtn;
    private EditText CarNoText;
    private TextView stationName,inTime;
    private ImageView carWhereImg;
    private LinearLayout stationInfo, stationInfo2;

    private String carNo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.activity_inquire_car, container, false);

        this.OKBtn = (Button) contentView.findViewById(R.id.OKBtn);
        this.CarNoText = (EditText) contentView.findViewById(R.id.carNoText);
        this.stationName = (TextView) contentView.findViewById(R.id.stationName);
        this.inTime = (TextView) contentView.findViewById(R.id.inTime);
        this.carWhereImg = (ImageView) contentView.findViewById(R.id.carWhereImg);
        this.stationInfo = (LinearLayout) contentView.findViewById(R.id.stationInfo);
        this.stationInfo2 = (LinearLayout) contentView.findViewById(R.id.stationIfo2);

        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.OKBtn.setOnClickListener( this.onOKBtnClickListener);
    }

    private Button.OnClickListener onOKBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(CarNoText.getText().length() >0){
                //API
                carNo = CarNoText.getText().toString();
                ApiLoaderCallback.startApiLoader(getActivity(), getCarWhereLoaderCallback);
            }else{
                int title = R.string.action_error;
                int content = R.string.action_error_content_carno;
                showAlert( title, content);
            }
        }
    };

    private void showAlert(int titleID, int contentID){
        AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity());
        dialog.setTitle( getString( titleID));
        dialog.setMessage( getString( contentID));

        dialog.setNegativeButton( "確認",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
            }

        });


        dialog.show();
    }

    //查詢鎖車狀態
    private ApiLoaderCallback<CW> getCarWhereLoaderCallback = new ApiLoaderCallback<CW>() {

        @Override
        public Loader<ApiResult<CW>> onCreateLoader(int id, Bundle args) {

            return new GetCarWhereLoader( getActivity(), carNo);
        }

        @Override
        protected void onStatusOk(CW data) {
            //Log.d("test", "onStatusOk: " + data.getResult_code());
            //Log.d("test", "onStatusOk: " + data.getNum());
            //Log.d("test", "onStatusOk: " + data.getImage_url());

            if(data.getResult_code().contains("OK")){
                stationInfo.setVisibility(View.VISIBLE);
                stationInfo2.setVisibility(View.VISIBLE);
                carWhereImg.setVisibility(View.VISIBLE);

                stationName.setText(data.getStation_name());
                inTime.setText(data.getIn_time());

                if(data.getImage_url2() != null && !data.getImage_url2().isEmpty()){
                    new DownloadImageTask(carWhereImg).execute(data.getImage_url2());
                }else{
                    new DownloadImageTask(carWhereImg).execute(data.getImage_url());
                }


            }else{
                if(data.getResult_code().contains("-99")){
                    int title = R.string.action_ca_error;
                    int content = R.string.action_ca_error_lock;
                    showAlert( title, content);
                }else{

                    int title = R.string.action_error;
                    int content = R.string.action_error_lock;
                    showAlert( title, content);
                }
            }
        }

        @Override
        protected void onPreLoadFinished() {

        }
    };

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }
}
