package ch.pizzalink.android.fragment;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalink.android.R;
import ch.pizzalink.android.api.ApiErrorUtils;
import ch.pizzalink.android.api.ApiService;
import ch.pizzalink.android.api.ResponseObject;
import ch.pizzalink.android.helper.DialogHelper;
import ch.pizzalink.android.model.StoreInfoModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by cimenmus on 18/09/2017.
 */

public class StoreInfoFragment extends BaseFragment {

    @BindView(R.id.storeInfoWebView) WebView storeInfoWebView;

    @BindString(R.string.bottom_nav_menu_item_info) String fragmentTitle;

    public static final java.lang.String FRAGMENT_NAME = "infoFragment";

    public StoreInfoFragment() {}

    public static StoreInfoFragment newInstance() {
        return new StoreInfoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_info, container, false);
        ButterKnife.bind(this, view);
        initViews();
        getStoreInfo();
        return view;
    }

    private void initViews(){
        setPizzalinkToolbarFields(false, "");
    }

    private void getStoreInfo(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject<StoreInfoModel>> call = ApiService.apiInterface.getStoreInfo();
        call.enqueue(new Callback<ResponseObject<StoreInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<StoreInfoModel>> call, Response<ResponseObject<StoreInfoModel>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.isSuccessful()){
                    initWebView(response.body().getData());
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject<StoreInfoModel>> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }

    private void initWebView(StoreInfoModel storeInfoModel) {
        storeInfoModel.checkNull();
        setPizzalinkToolbarFields(false, storeInfoModel.getTitle());
        storeInfoWebView.getSettings().setJavaScriptEnabled(true);
        storeInfoWebView.loadDataWithBaseURL("", storeInfoModel.getContent(), "text/html", "UTF-8", "");

    }

}
